java POI动态添加修改Excel,表头是动态的。急啊

java POI 首先读取模板,修改添加数据,表头是动态的,
要写一个公共类,请教下大神。
/**
* @param path 模板路径
* @param esheet 表
* @param dataList 数据
* @return 结果集
* @throws IOException
*/
@SuppressWarnings(“all”)
public static String writerExcel(String path, int esheet, List dataList) throws Exception {
// ClassPathResource resource = new ClassPathResource("/");
// String sysPath = “”;
// try {
// sysPath = resource.getURI().getPath();
// } catch (Exception e) {
// e.printStackTrace();
// }
// String templateFilePath = sysPath + “templates/采购办.xls”;
// System.out.println(“相对路径”+templateFilePath );
//路径
String pathType = path.substring(path.lastIndexOf("."));
Iterator it = (Iterator) dataList.iterator();
FileInputStream fileInputStream = new FileInputStream(path); //建立数据的输入通道
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fileInputStream); //使用POI提供的方法得到excel的信息
Workbook web = null;
if (Excel_2003.equals(pathType)) {
web = new HSSFWorkbook(poifsFileSystem);//得到文档对象
} else if (Excel_2007.equals(pathType)) {
web = new XSSFWorkbook(fileInputStream);

    } else {
        throw new Exception("文件格式有误!!!");
    }

    Sheet sheet = web.getSheetAt(esheet);  //获取sheet表
 
    Row dataRow = sheet.getRow(6);  //获取第一行
    int row = dataRow.getRowNum() + 1;
    out.println("下一行的行号" + row);

    out.println(sheet.getLastRowNum() + " " + dataRow.getLastCellNum());  //分别得到最后一行的行号,和一条记录的最后一个单元格
    out.println(sheet.getLastRowNum() + "总行数");
    out.println("每一行列数" + dataRow.getLastCellNum());

    int num = sheet.getLastRowNum() - 12;
    FileOutputStream out = new FileOutputStream(path);  //向.xls中写数据 , 打开流
    //插入需导出的数据
    int k = 7;
    //循环每一行
    for (int i = 0; i < dataList.size(); i++) {
        dataRow = sheet.getRow(k++);
        System.out.println("每一行记录" + dataList.get(i));

// //属性名称,对应的值
Map object = BeanCopyUtil.getAllPropertiesValue(dataList.get(i));
System.out.println(object.values());
System.out.println(object.keySet());
System.out.println(object.size());
Collection map = object.values();
for (String key : object.keySet()) {
System.out.println(“k值:” + key + “value值” + object.get(key));
}
dataList.get(i);
List elist = new ArrayList();
elist.add(dataList.get(i));

        System.out.println(object);
        // 循环每一列

        for (int j = 0; j < dataRow.getLastCellNum(); j++) {
            System.out.println(object.values());

            dataRow.getCell(j).setCellValue(2);
            for(String key:object.keySet()){
                System.out.println("k值:"+key+"value值"+object.get(key));

                System.out.print("j==="+j);
                if(object.get(key) instanceof BigDecimal){

// dataRow.getCell(j).setCellValue(((BigDecimal) object.get(key)).doubleValue());
dataRow.getCell(j).setCellValue(“10”);
System.out.println("((BigDecimal) object1).doubleValue()"+((BigDecimal) object.get(key)).doubleValue());
}else if(object.get(key) instanceof String){
dataRow.getCell(j).setCellValue(object.get(key).toString());
System.out.println("object1.toString()
"+object.get(key).toString());

                }else{
                    dataRow.getCell(j).setCellValue(5);
                    System.out.println();
                }



            }

        }

你可能感兴趣的:(java POI动态添加修改Excel,表头是动态的。急啊)