我想在现有的Excel工作表中添加单元格但是我收到此错误jxl.read.biff.BiffException:

时间:2016-03-29 19:31:24

标签: java excel

File iwb = new File("D:\\f.xls");
        Workbook wb;
        try
        {
         wb = Workbook.getWorkbook(iwb); 
        WritableWorkbook copy = Workbook.createWorkbook(new File("D:\\f.xls"),wb);
        WritableSheet sheet = copy.getSheet(0); 

        jxl.write.Label label,label1,label2,label3;
        int i=sheet.getRows();
        int j=sheet.getColumns();
            System.out.println(i+j);
        label = new jxl.write.Label(0, 0, "N0.");
        label1 = new jxl.write.Label(1, 0, "Name");
        label2 = new jxl.write.Label(4, 0, "PIN");
        label3 = new jxl.write.Label(6, 0, "Date");
        sheet.addCell(label);
        sheet.addCell(label1);
        sheet.addCell(label2);
        sheet.addCell(label3);

        copy.write(); 
        copy.close();
        } catch (IOException ex) {
            Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (WriteException ex) {
            Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BiffException ex) {
            Logger.getLogger(test1.class.getName()).log(Level.SEVERE, null, ex);
        }

1 个答案:

答案 0 :(得分:0)

我建议使用apache poi库来执行此操作。 下面是一个示例代码,它使用apache poi xssf从包含三列中的员工数据的文本文件生成excel,即Title,Name,Location。 (这三个字段由分隔符夸大' - '在文本文件中)

try {
        String DIR_PATH = "D:\\f.txt";
        FileReader read = new FileReader(new File(DIR_PATH));
        BufferedReader bfrRead = new BufferedReader(read);
        String oneLine = null;
        //Blank workbook
        XSSFWorkbook workbook = new XSSFWorkbook();

        //Create a blank sheet
        XSSFSheet sheet = workbook.createSheet("Employee Data");
        int rownum = 0;
        Row row0 = sheet.createRow(rownum++);
        Cell cell01 = row0.createCell(0);
        cell01.setCellValue("Title");
        Cell cell02 = row0.createCell(1);
        cell02.setCellValue("Name");
        Cell cell03 = row0.createCell(2);
        cell03.setCellValue("Location");
        while((oneLine = bfrRead.readLine())!=null){
            if(oneLine.isEmpty())
                continue;
            Row row = sheet.createRow(rownum++);


            StringBuilder title = new StringBuilder();
            StringBuilder name= new StringBuilder();
            StringBuilder loc = new StringBuilder();
            int switch1 = 1;

            //logic to parse from text file
            for(int i = 0; i < oneLine.length(); i++){
                if(oneLine.charAt(i) != '-'){
                    if(switch1 == 1)
                        title.append(oneLine.charAt(i));
                    else if(switch1 == 2)
                        name.append(oneLine.charAt(i));
                    else if (switch1 == 3)
                        loc.append(oneLine.charAt(i));
                }
                else 
                    if(oneLine.charAt(i+1) == '-'){
                        i=i+1;
                        switch1++;
                    }
                    else
                        switch1++;

            }
            Cell cell1 = row.createCell(0);
            cell1.setCellValue((String)title.toString());
            Cell cell2 = row.createCell(1);
            cell2.setCellValue((String)name.toString());
            Cell cell3 = row.createCell(2);
            cell3.setCellValue((String)loc.toString());
        }
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File("D:\\f.xlsx"));
        workbook.write(out);
        out.close();
        bfrRead.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }