小编典典

Java如何读写Excel文件

java

我想用3列N行从Java读写Excel文件,在每个单元格中打印一个字符串。谁能给我简单的代码片段吗?我是否需要使用任何外部库,或者Java是否内置支持?

我要执行以下操作:

for(i=0; i <rows; i++)
     //read [i,col1] ,[i,col2], [i,col3]

for(i=0; i<rows; i++)
    //write [i,col1], [i,col2], [i,col3]

阅读 439

收藏
2020-02-28

共1个答案

小编典典

Apache POI可以为你做到这一点。特别是HSSF模块。该快速指南是最有用的。这是你想做什么的方法-专门创建一张纸并将其写出来。

Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short)0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);

// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
2020-02-28