简体   繁体   English

使用Apache POI写入Excel

[英]Write to excel using Apache POI

Earlier I was using SAX parser for export to excel. 早些时候,我使用SAX解析器导出到excel。 Now I am not able to get data using Apache POI, Any suggestion??? 现在我无法使用Apache POI获取数据,有什么建议吗??? This String in XML contains all the data which I retrieved through farpoint grid tech and set in the form in action class. XML中的String包含了我通过farpoint网格技术检索到的所有数据,并在action类的表单中进行了设置。

private void parseXML(String inXML) {
    /* // get a factory
    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        // get a new instance of parser
        SAXParser sp = spf.newSAXParser();

        // parse the file
        sp.parse(new InputSource(new StringReader(inXML)), this);    
    } catch (IOException ie) {          
}*/

Now I am using this. 现在我正在使用它。

 HSSFWorkbook workbook = new HSSFWorkbook();
 HSSFSheet sheet = workbook.createSheet("Sample sheet");
 short rowNum = 0;
 short colNum = 0;

 Row row = sheet.createRow(rowNum++);
 Cell cell = row.createCell(colNum);
 cell.setCellValue(inXML);

 **/* Iterator<Row> rowIterator = sheet.iterator();
 while(rowIterator.hasNext()) {
     Row row = rowIterator.next();

     //For each row, iterate through each columns
     Iterator<Cell> cellIterator = row.cellIterator();
     while(cellIterator.hasNext()) {                       
         Cell cell = cellIterator.next();
         cell.setCellValue(inXML);

         switch(cell.getCellType()) {
             case Cell.CELL_TYPE_BOOLEAN:
                 System.out.print(cell.getBooleanCellValue() + "\t\t");
                 break;
             case Cell.CELL_TYPE_NUMERIC:
                 System.out.print(cell.getNumericCellValue() + "\t\t");
                 break;
             case Cell.CELL_TYPE_STRING:
                 System.out.print(cell.getStringCellValue() + "\t\t");
                 break;
         }
     }*/**

     try {
         FileOutputStream out = new FileOutputStream(new File("C:\\Excel.xls"));
         workbook.write(out);
         out.close();                                     
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }    
}

This program (which is what you posted with added imports, without the comment block, with a literal string instead of the variable inXML and with the path removed from the output file): 该程序(这是您使用添加的导入发布的内容,没有注释块,使用文字字符串而不是变量inXML并从输出文件中删除了路径):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.ss.usermodel.Cell;

public class Main {
    public Main() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sample sheet");
        short rowNum = 0;
        short colNum = 0;

        HSSFRow row = sheet.createRow(rowNum++);
        Cell cell = row.createCell(colNum);
        cell.setCellValue("<data>test data</data>");

        try {
            FileOutputStream out = new FileOutputStream(new File("Excel.xls"));
            workbook.write(out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Produced exactly what you would expect: A spreadsheet with <data>test data</data> in cell A1. 完全符合您的期望:在A1单元格中包含<data>test data</data>电子表格。

Perhaps cell A1 in your spreadsheet is blank because inXML really is blank. 电子表格中的单元格A1可能是空白的,因为inXML实际上是空白的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM