无法使用带有shiftrows的Apache POI SXSSFWorkbook类创建excel

时间:2015-02-18 10:16:27

标签: java excel apache apache-poi

我正在尝试使用Apache POI SXSSFWorkbook类创建一个excel工作簿。但如果我使用下面示例代码中给出的shiftrows方法,则无法运行该文件。

package com.howtodoinjava.demo.poi;

import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.ss.usermodel.FormulaEvaluator;

public class sample {

    public static void main(String[] args) {
        //Blank workbook
        SXSSFWorkbook workbook = new SXSSFWorkbook();

        //Create a blank sheet
        Sheet sheet = workbook.createSheet("Employee Data");

        //This data needs to be written (Object[])
        LinkedHashMap<String, Object[]> data = new LinkedHashMap<String, Object[]>();
        data.put("1", new Object[]{"NAME", "LASTNAME", "Count"});
        data.put("2", new Object[]{"Amit", "Shukla", 1});
        data.put("13", new Object[]{"Ram", "Krish", 2});

        //Iterate over data and write to sheet
        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset) {
            Row row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) {
                    cell.setCellValue((String) obj);
                } else if (obj instanceof Integer) {
                    cell.setCellValue((Integer) obj);
                }
            }
        }
        try {

             sheet.shiftRows(1, sheet.getLastRowNum(), 1, false, false);
                      Row insRow = sheet.createRow(1);
                       for (int k = 0; k < sheet.getRow(1).getLastCellNum(); k++) {
                            Cell insCell = insRow.createCell(k);
                            if (k == 0) {
                                insCell.setCellValue("Govind");
                            } else if (k == 1) {
                                insCell.setCellValue("Narayan");
                            } else {
                                insCell.setCellType(Cell.CELL_TYPE_NUMERIC);
                                insCell.setCellFormula("7");
                            }
                        }

            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File("excelSample.xlsx"));
            workbook.write(out);
            out.close();

            System.out.println("excelSample.xlsx written successfully on disk.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getColumnName(int i) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

但它会抛出java.lang.RuntimeException: NotImplemented at org.apache.poi.xssf.streaming.SXSSFSheet.shiftRows(SXSSFSheet.java:827) at com.howtodoinjava.demo.poi.sample.main(sample.java:50)错误消息。有什么工作?

0 个答案:

没有答案