Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置列名_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何设置列名

Java 如何设置列名,java,excel,apache-poi,Java,Excel,Apache Poi,我正在为我的应用程序使用ApachePOI,我想设置列名称,而不是“A、B、C…” 我使用以下代码,但它只在第一行添加数据: public void createHeaderRow(HSSFSheet sheet, List<String> headerRowList) { HSSFRow headerRow = sheet.createRow(0); for(int i=0;i<headerRowList.size();i++){ HSSF

我正在为我的应用程序使用ApachePOI,我想设置列名称,而不是“A、B、C…”

我使用以下代码,但它只在第一行添加数据:

 public void createHeaderRow(HSSFSheet sheet, List<String> headerRowList) {
    HSSFRow headerRow = sheet.createRow(0);
    for(int i=0;i<headerRowList.size();i++){
        HSSFCell cell = headerRow.createCell((short) i);
        cell.setCellValue(headerRowList.get(i).toString());
    }
}
public void createHeaderRow(HSSF表单,列表headerRowList){
HSSFRow headerRow=sheet.createRow(0);

对于(int i=0;i坏消息:AFAIK excel不能执行与A、B、C不同的操作。主要原因:列和行名称不包含信息,只是编号。假设您要打印包含这些名称的文档:excel不知道格式,也不知道边框,甚至不知道单元格宽度/高度

您有以下选项:

  • 使用MS Access,它使用数据库布局(例如字段名作为列名)
  • 修复视图中的第一行并将其格式化为表标题。Excel的表自动格式化功能也采用相同的方式
    • 例如:

      JFileChooser save = new JFileChooser();
              save.setDialogTitle("Save file");
              int chooise = save.showSaveDialog(null);
      
              File file = null;
              if(chooise == JFileChooser.APPROVE_OPTION){
                  file = save.getSelectedFile();
              }
      
              FileOutputStream fileOut;
              fileOut = new FileOutputStream(file.getAbsolutePath() + ".xls");
              HSSFWorkbook workbook = new HSSFWorkbook();
              HSSFSheet worksheet = workbook.createSheet("Workbook");
              Row row1 = worksheet.createRow((short)0);
              //Set columt names
              row1.createCell(0).setCellValue("Column Name 0");
              row1.createCell(1).setCellValue("Column Name 1");
      
      等等