使用Java POI读取数据并将其输入新的Excel文件

时间:2015-12-14 15:31:41

标签: java excel apache-poi

我正在尝试在java中读取excel文件,修改它然后将其放入新的excel文件中。在那一刻我只是试图将读取数据直接放入一个新文件。我的问题是如何将cell.getContents()中的内容直接导入新文件

if(!f.exists()){

            String FileName = "C:\\Users\\alexp\\Desktop\\New_Export.xls";
            WritableWorkbook excel = Workbook.createWorkbook(new File(FileName));
            WritableSheet sheet = excel.createSheet("Sheet1", 0);                                           

                                    for (int y = 0; y < page.getRows(); y++){

                                        for (int x = 0; x < page.getColumns(); x++){

                                            Cell cell = page.getCell(x ,y +1);
                                            CellType type = cell.getType();

                                                    if(type == CellType.LABEL){

                                                        System.out.println(cell.getContents());
                                                        Label label1 = new Label();
                                                        sheet.addCell(label1);
                                                        if(type == CellType.NUMBER){

                                                            System.out.println(cell.getContents());
                                                            Number num1 = new Number ();
                                                            sheet.addCell(num1);                                                                                

                                                        }           
                                                   }        
                                        }

                                        System.out.println(" ");

                                    }   


            System.out.println("The File Has Successfully Been Created!");

            excel.write();
            excel.close();

            }
            else{

                System.out.println("File is already created!");

            }

1 个答案:

答案 0 :(得分:0)

您可以使用apache poi读取Excel工作表的每一行,如下所示:

HssfRow row = sheet.getRow(rowIndex);
HssfCell cell = row.getCell(columnIndex);
FormulaEvaluator fe = workbook.getCreationHelper().createFormulaEvalator();
CellValue cv = fe.evaluate(cell);

希望这会对你有帮助,我已经从手机输入了这段代码,你可能会得到一些语法错误,纠正它们会起作用。祝你有愉快的一天: - )