2010-08-11 11 views
5

क्या कोई मुझे जावा में एक्सेल फ़ाइल लिखने के लिए सही दिशा में इंगित कर सकता है ?? मैं ऑनलाइन मिले लिंक को समझ नहीं रहा हूं। क्या आप मुझे सिर्फ एक लिंक या कुछ भी भेज सकते हैं जिसे मैं अनुसरण कर सकता हूं ??जावा में उत्कृष्टता के लिए लेखन

धन्यवाद, जम्मू

+0

क्या लिंक आप के बारे में बात कर रहे हैं? जावा से एक्सेल फ़ाइलों को लिखने के बारे में जाने के कई अलग-अलग तरीके हैं। क्या कोई विशेष एपीआई या लाइब्रेरी है जिसके साथ आपको समस्याएं आ रही हैं? – FloppyDisk

+0

[जेएसपी जेनरेट करने के लिए एक्सेल स्प्रेडशीट (एक्सएलएस) उत्पन्न करने के संभावित डुप्लिकेट] (http://stackoverflow.com/questions/477886/jsp-generating-excel-spreadsheet-xls-to-download) –

उत्तर

12

Apache POI के लिए एक अन्य विकल्प JExcelAPI है, जो (IMO) एक आसान एपीआई का उपयोग करना पड़ता है। Some examples:

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls")); 

WritableSheet sheet = workbook.createSheet("First Sheet", 0); 

Label label = new Label(0, 2, "A label record"); 
sheet.addCell(label); 

Number number = new Number(3, 4, 3.1459); 
sheet.addCell(number); 
+0

बहुत बढ़िया API। अपाचे पीओआई से बहुत आसान है। धन्यवाद! – JCab

0

मैं का उपयोग किया है Apache's POI Library जब मैं जावा से फाइल उत्कृष्टता प्राप्त करने लिखने के लिए मिला है। एक बार जब आप इसे लटका लेंगे तो मैंने इसे सीधे आगे बढ़ाया। जावा वर्ल्ड में पीओआई का उपयोग शुरू करने के बारे में एक अच्छा tutorial है जो मुझे बहुत उपयोगी पाया गया।

0

:

http://www.vogella.de/articles/JavaExcel/article.html

http://www.java-tips.org/other-api-tips/jexcel/how-to-create-an-excel-file.html

POI (थोड़ा पुराने लेकिन उपयोगी) का उपयोग कर के लिए कदम उदाहरण के द्वारा कदम मैंने जेएक्ससेलएपीआई का इस्तेमाल किया है और हो गया है this tutorial से डब्ल्यू।

1

यहाँ मैं नमूना उदाहरण देने के लिए जा रहा हूँ, कैसे एक्सेल लिखने के लिए;

<dependency> 
    <groupId>net.sf.jxls</groupId> 
    <artifactId>jxls-core</artifactId> 
    <version>0.9</version> 
</dependency> 

मॉडल वर्ग pom.xml में इस का उपयोग करें:

public class Products {  
    private String productCode1;  
    private String productCode2;  
    private String productCode3; 

    constructors,setters and getters  
} 

मुख्य वर्ग:

public class WriteExcel { 

    public void writeExcel() { 

     Collection staff = new HashSet();  

     staff.add(new Products("101R15ss0100", "PALss Kids 1.5 A360 ", "321"));  
     staff.add(new Products("101R1ss50100", "PAL sKids 1.5 A360 ", "236"));  

     Map beans = new HashMap();  

     beans.put("products", staff);  
     XLSTransformer transformer = new XLSTransformer();  

     try {  
      transformer.transformXLS(templateFileName, beans, destFileName); 

      System.out.println("Completed!!");  

     } catch (ParsePropertyException e) {  
      System.out.println("In ParsePropertyException");  
      e.printStackTrace();  
     } catch (IOException e) {  
      System.out.println("In IOException");  
      e.printStackTrace();  
     } 
    } 

    public static void main(String[] args) {  
     WriteExcel writeexcel = new WriteExcel();  
     writeexcel.writeExcel();  
    }  

}  
-1
public class ExcelUtils { 

    private static XSSFSheet ExcelWSheet; 
    private static XSSFWorkbook ExcelWBook; 
    private static XSSFCell Cell; 
    private static XSSFRow Row; 
    File fileName = new File("C:\\Users\\satekuma\\Pro\\Fund.xlsx"); 

    public void setExcelFile(File Path, String SheetName) throws Exception { 
     try { 
      FileInputStream ExcelFile = new FileInputStream(Path); 
      ExcelWBook = new XSSFWorkbook(ExcelFile); 
      ExcelWSheet = ExcelWBook.getSheet(SheetName); 
     } catch (Exception e) { 
      throw (e); 
     } 
    } 


    public static String getCellData(int RowNum, int ColNum) throws  Exception { 
     try { 
      Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); 
      String CellData = Cell.getStringCellValue(); 
      return CellData; 
     } catch (Exception e) { 
      return ""; 
     } 
    } 

    public static void setCellData(String Result, int RowNum, int ColNum, File Path) throws Exception { 
     try { 
      Row = ExcelWSheet.createRow(RowNum - 1); 
      Cell = Row.createCell(ColNum - 1); 
      Cell.setCellValue(Result); 
      FileOutputStream fileOut = new FileOutputStream(Path); 
      ExcelWBook.write(fileOut); 
      fileOut.flush(); 
      fileOut.close(); 
     } catch (Exception e) { 
      throw (e); 
     } 
    } 
} 
1

एक्सेल फ़ाइल (.xls) में डेटा लिखने का मूल कोड यहां दिया गया है।

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFCellStyle; 
import org.apache.poi.hssf.usermodel.HSSFDataFormat; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.hssf.util.HSSFColor; 

public class Export2Excel { 

    public static void main(String[] args) { 
     try { 
      //create .xls and create a worksheet. 
      FileOutputStream fos = new FileOutputStream("D:\\data2excel.xls"); 
      HSSFWorkbook workbook = new HSSFWorkbook(); 
      HSSFSheet worksheet = workbook.createSheet("My Worksheet"); 

      //Create ROW-1 
      HSSFRow row1 = worksheet.createRow((short) 0); 

      //Create COL-A from ROW-1 and set data 
      HSSFCell cellA1 = row1.createCell((short) 0); 
      cellA1.setCellValue("Sno"); 
      HSSFCellStyle cellStyle = workbook.createCellStyle(); 
      cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); 
      cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
      cellA1.setCellStyle(cellStyle); 

      //Create COL-B from row-1 and set data 
      HSSFCell cellB1 = row1.createCell((short) 1); 
      cellB1.setCellValue("Name"); 
      cellStyle = workbook.createCellStyle(); 
      cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index); 
      cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
      cellB1.setCellStyle(cellStyle); 

      //Create COL-C from row-1 and set data 
      HSSFCell cellC1 = row1.createCell((short) 2); 
      cellC1.setCellValue(true); 

      //Create COL-D from row-1 and set data 
      HSSFCell cellD1 = row1.createCell((short) 3); 
      cellD1.setCellValue(new Date()); 
      cellStyle = workbook.createCellStyle(); 
      cellStyle.setDataFormat(HSSFDataFormat 
        .getBuiltinFormat("m/d/yy h:mm")); 
      cellD1.setCellStyle(cellStyle); 

      //Save the workbook in .xls file 
      workbook.write(fos); 
      fos.flush(); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

नोट:आप लिंक से जार पुस्तकालय डाउनलोड करना होगा:http://www.java2s.com/Code/JarDownload/poi/poi-3.9.jar.zip

संबंधित मुद्दे