当前位置: 首页 > 知识库问答 >
问题:

如何使用Apache POI重写. xlsm文件?

袁开宇
2023-03-14

我使用这段代码使用ApachePOI重写xlsm文件。我必须从resultset重写Sheet1上的数据,这段代码创建模板xlsm文件的副本,并执行所有处理。

但当我打开创建的xlsm文件时,会显示以下消息:

我们发现“文件名”中的某些内容有问题。xlsm’。你想让我们尽力恢复吗?如果您信任此工作簿的来源,请单击“是”。

这是我的代码,请建议我该怎么做。

public void dbConnect(String driver_connect_string, String db_connect_string, String db_userid, String db_password){
  try{ 
    Class.forName(driver_connect_string);
    Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
    System.out.println("connected");
    Statement statement = conn.createStatement();
    Properties propq = new Properties();    
    FileInputStream fisq = new FileInputStream("query.properties");
    propq.load(fisq);
    String queryString = propq.getProperty("myQueryString");
    ResultSet rs = statement.executeQuery(queryString);

    Properties propf2 = new Properties();   
    FileInputStream fisf2 = new FileInputStream("file.properties");
    propf2.load(fisf2);

    OPCPackage pkg = OPCPackage.open(new File(propf2.getProperty("sourceFile")));
    XSSFWorkbook wb_template;
    wb_template = new XSSFWorkbook(pkg);
    System.out.println("package loaded");
    SXSSFWorkbook wb = new SXSSFWorkbook(wb_template); 
    wb.setCompressTempFiles(true);

     Sheet  sheet = wb.getSheetAt(0); 
     Row rowhead = sheet.createRow((short) 0);
         rowhead.createCell((short) 0).setCellValue("EmpId");
         rowhead.createCell((short) 1).setCellValue("EmaName");
         rowhead.createCell((short) 2).setCellValue("Department");
         rowhead.createCell((short) 3).setCellValue("Job Title");
         rowhead.createCell((short) 4).setCellValue("DOB");
         int index = 1;
         while (rs.next()) {
              Row row = sheet.createRow((short) index);
              row.createCell((short) 0).setCellValue(rs.getString(1));
              row.createCell((short) 1).setCellValue(rs.getString(2));
              row.createCell((short) 2).setCellValue(rs.getString(3));
              row.createCell((short) 3).setCellValue(rs.getString(4));
              row.createCell((short) 4).setCellValue(rs.getString(5));
              index++;
          }


         FileOutputStream out = new FileOutputStream(new File(propf2.getProperty("destFile")));
         System.out.println("XLSM created Successfully");
         wb.write(out);
         out.close();  
    }catch(Exception e){
      e.printStackTrace();
  }
}

共有1个答案

董翰池
2023-03-14

完成。。。。。。。

public void dbConnect(String driver_connect_string, String db_connect_string, String db_userid, String db_password){
 try{ 
  Class.forName(driver_connect_string);
  Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
  System.out.println("connected");
  Statement statement = conn.createStatement();
  Properties propq = new Properties();    
  FileInputStream fisq = new FileInputStream("query.properties");
  propq.load(fisq);
  String queryString = propq.getProperty("myQueryString");
  ResultSet rs = statement.executeQuery(queryString);

  Properties propf2 = new Properties();   
  FileInputStream fisf2 = new FileInputStream("file.properties");
  propf2.load(fisf2);

  OPCPackage pkg = OPCPackage.open(new File(propf2.getProperty("sourceFile")));
  XSSFWorkbook wb_template;
  wb_template = new XSSFWorkbook(pkg);
  System.out.println("package loaded");


  Sheet  sheet = wb_template.getSheetAt(0);

  Row rowhead = sheet.createRow(0);
     rowhead.createCell(0).setCellValue("EmpId");
     rowhead.createCell(1).setCellValue("EmaName");
     rowhead.createCell(2).setCellValue("Department");
     rowhead.createCell(3).setCellValue("Job Title");
     rowhead.createCell(4).setCellValue("DOB");
     int index = 1;
     while (rs.next()) {
          Row row = sheet.createRow((short) index);
          row.createCell(0).setCellValue(rs.getString(1));
          row.createCell(1).setCellValue(rs.getString(2));
          row.createCell(2).setCellValue(rs.getString(3));
          row.createCell(3).setCellValue(rs.getString(4));
          row.createCell(4).setCellValue(rs.getString(5));
          index++;
      }


     FileOutputStream out = new FileOutputStream(new File(propf2.getProperty("destFile")));
     System.out.println("XLSM created Successfully");
     wb.write(out);
     out.close();  
  }catch(Exception e){
    e.printStackTrace();
  }
}
 类似资料:
  • 我已经编写了用于编写xlsm(Excel2007)的java文件。 使用ApachePOI库,编写xlsx文件是成功的。编写xlsm文件是成功的。但我无法打开xlsm文件,因为打开xlsm文件时出错。 使用ApachePOI库编写xlsm文件可行吗? 如果可以编写xlsm,请提供如何使用ApachePOI库编写xlsm文件的指南。 谢谢

  • 问题内容: 我已经编写了用于编写xlsm(Excel 2007)的Java文件。 使用Apache POI库,成功编写xlsx文件。并且编写xlsm文件是成功的。但是由于打开xlsm文件时出错,我无法打开xlsm文件。 使用Apache POI库编写xlsm文件是否可行? 如果编写xlsm可行,请提供指南如何使用Apache poi库编写xlsm文件。 谢谢 问题答案: 根据Apache POI的

  • wef:使用apache poi写入xlsm(Excel 2007) 当我向文件中写入一个简单的字符串时,我无法打开该文件。错误-“Excel无法打开文件“Test1.xlsm”,因为文件格式或文件扩展名无效”

  • 我正在处理一个大型的excel文件,并从apache poi event user model(万圣节文档)中获取参考。http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api。xlsm文件如下所示 所以我的目的是跳过我标记的单元格,即从1到6行开始,我想跳过 这样我就可以得到excel文件中的行号。 但是这个api如何处理每一行我不知道

  • 我正在写一个程序,它需要从excel文件中读取和写入数据,而不考虑格式(xls或xlsx)。 我知道ApachePOI,但它似乎有不同的类来处理xls文件(HSSF)和xlsx(XSSF)文件。 任何人都知道我将如何实现我在这里的目标。(也欢迎使用POI以外的API的想法)。

  • 如何使用Apache POI使整个excel行单元格加粗文本? 例如: 列标题应该用粗体。与其为标题行的每个单元格应用样式,我如何将一些样式应用于整行?