5
A
回答
6
爲了不被平庸,但是Apache POI可以做到這一點。你可以在這裏找到一些代碼示例:
http://poi.apache.org/spreadsheet/examples.html
12
到Apache POI另一種方法是JExcelAPI,它(IMO)有一個易於使用的API。 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。比Apache POI簡單得多。謝謝! – JCab 2013-02-17 18:02:29
0
我用Apache's POI Library當我不得不寫從Java Excel文件。一旦你掌握了它,我發現它很直接。 Java World有一個很好的tutorial關於開始使用我覺得非常有幫助的POI。
3
0
我已經使用JExcelAPI並學會了ho w從this tutorial。
1
在這裏,我想給樣本爲例,介紹如何編寫Excel中;在pom.xml中使用此
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>0.9</version>
</dependency>
模型類:
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
這裏是在excel文件(.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();
}
}
}
注:你需要從鏈接下載JAR庫:http://www.java2s.com/Code/JarDownload/poi/poi-3.9.jar.zip
相關問題
- 1. 在Excel中寫入Excel文件#
- 2. 在Java GUI中嵌入Excel
- 3. 閱讀文本並在Java中寫入Excel中
- 4. 用apache poi從Java寫入Excel文件
- 5. 將數據寫入Excel工作表java
- 6. Java - 無法寫入excel單元
- 7. Selenium Java - 將hashmap值寫入excel
- 8. 如何在Excel中將數字數組寫入Excel中Excel
- 9. 如何在java中讀取/寫入excel複選框狀態
- 10. 在java中使用Apache POI讀取和寫入excel文件
- 11. 在java中寫入excel時出現錯誤
- 12. 在Java中寫入串口
- 13. 在Java中寫入資源?
- 14. 在Java中寫入unicode 0x2
- 15. java在cmd中寫入netstat
- 16. 將.txt寫入.csv Excel中的excel列
- 17. 讀取和寫入到Excel中的Excel#
- 18. 從Python寫入Excel到Excel
- 19. VBScript寫入Excel不寫
- 20. 在python中寫入excel csv的值
- 21. 使用python在excel中寫入多行
- 22. 在Excel中寫入IF語句
- 23. 在Excel中寫入的函數
- 24. 在Excel工作表中寫入內容
- 25. 在C#中將字典寫入Excel#
- 26. 在Excel表格中寫入查詢c#
- 27. 在Excel文件中寫入宏的VBScript
- 28. 將HashMap寫入Excel
- 29. 寫入Excel對象
- 30. SSIS不寫入Excel
你在說什麼聯繫?有很多不同的方式可以用來從Java寫入excel文件。是否有特定的API或庫存在問題? – FloppyDisk 2010-08-11 03:27:28
[JSP生成Excel電子表格(XLS)下載]的可能重複(http://stackoverflow.com/questions/477886/jsp-generating-excel-spreadsheet-xls-to-download) – 2010-08-11 03:35:07