簡體   English   中英

使用Java POI讀取數據並將其輸入到新的Excel文件中

[英]Reading data and inputting it into a new Excel file using Java POI

我目前正在嘗試在Java中讀取Excel文件,對其進行修改,然后將其放入新的Excel文件中。 即時消息只是試圖將讀取的數據直接放入新文件中。 我的問題是如何從cell.getContents()獲取內容到新文件中

如果(!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!");

            }

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

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

希望這對您有幫助,我已經從移動設備上鍵入了此代碼,您可能會遇到一些語法錯誤,使它們可以正常工作。 祝你今天愉快:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM