我有一个EXCEL模板,因为模板中其他数据是有用的,我只是想在其中的固定单元格写入数据,模板数据是单元格样式的,就是把EXCEL模板数据读出来,然后写到新的EXCEL中,而且新生成的EXCEL固定单元格里需要输入我想输入的数据。。 能帮我写段代码吗?挺着急啊~java

解决方案 »

  1.   

    带样式的太麻烦了,不行。
    其他的可以。
    下面是我写的一段工具类代码// 该方法实现的功能是读,读的时候全部以String的方式存储,如果小数点后面为0的全部忽略,
    public static String[][] poiReader(String filepath, File file) { String[][] result;
    InputStream is = null;
    try {
    if (filepath!=null&&!filepath.equals("")) {
    // 输入输出流
    is = new FileInputStream(filepath);

    else if (file.exists()) {
    is = new FileInputStream(file);

    } else {
    System.out.println("输入的路径并且文件为空");
    }
    }catch (FileNotFoundException e) {
    e.printStackTrace();
    } // 创建工作空间
    Workbook wb = null;
    try {
    wb = WorkbookFactory.create(is);
    } catch (Exception e) {
    e.printStackTrace();

    // 获取工作表
    Sheet sheet = wb.getSheetAt(0);// 获取第一个工作表
    // 工作行
    Row row;
    // 工作单元格
    Cell cell = null;
    int rownum;// 行
    int columnnum;// 列
    rownum = sheet.getLastRowNum()+1;
    columnnum = sheet.getRow(0).getLastCellNum();
    // 实例化返回的数组对象
    result = new String[rownum][columnnum]; System.out.println("rownum:" + rownum);
    System.out.println("columnnum:" + columnnum);
    for (int i = 0; i < rownum; i++) {
    row = sheet.getRow(i); for (int j = 0; j < columnnum; j++) {
    cell = row.getCell(j);
    if (cell == null) {
    String str = "";
    result[i][j] = str;
    } else {
    String str = cell.toString();
    if (str.contains(".") && str.matches("[0-9]{1,}.[0-9]{1,}")) {
    String[] s = str.split("\\.");
    if (Integer.parseInt(s[1]) == 0) {
    result[i][j] = s[0];
    } else {
    result[i][j] = str;
    }
    } else {
    result[i][j] = str;
    }
    }
    }
    }
    return result;
    } // 输入二维数组和文件存放的地址,该方法把数组中的内容(包括表头)存入execl文件,返回文件存放地址
    // 注:方法本身没有判断文件是否属于execl文件的功能,存的时候不附带任何格式。
    public static String poiWrite(String[][] str, String filepath) {
    InputStream inp;
    ifexist(filepath);
    try {
    inp = new FileInputStream(filepath);
    int rownum = str.length;
    int columnum = str[0].length; Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(0);
    for (int i = 0; i < rownum; i++) {
    // System.out.println("i:"+i);
    Row row = sheet.createRow(i);
    for (int j = 0; j < columnum; j++) {
    /* System.out.println("j:"+j); */
    Cell cell = row.createCell(j);
    // 设置格式
    cell.setCellType(Cell.CELL_TYPE_STRING);
    if(str[i][j]!=null){
    // 设置值
    cell.setCellValue(str[i][j]);
    }else{
    cell.setCellValue("");
    }
    }
    }
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream(filepath);
    wb.write(fileOut);
    fileOut.close();
    inp.close();
    System.out.println("写入完成,路径为" + filepath);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return filepath;
    }