简体   繁体   English

Java 创建 excel 文件,但我无法打开该文件

[英]Java creates excel file but i am not able to open the file

i have this java code which should create xls file but the file cannot be open.我有这个 java 代码,它应该创建 xls 文件,但文件无法打开。 Excel says the format of the file is not valid. Excel 说文件格式无效。 I added all librairie for generating xlsx file, am i missing something here.我添加了所有用于生成 xlsx 文件的库,我在这里遗漏了什么。 Any help please Thank you任何帮助请谢谢

Here is the code:这是代码:

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.collect.Table.Cell;
import jxl.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;           

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Trade");
Map < String, Object[] > data = new TreeMap < String, Object[] > ();
data.put("1", new Object[] {
    "Code"
});

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) {
        org.apache.poi.ss.usermodel.Cell cell = row.createCell(cellnum++);
        if (obj instanceof String)
            cell.setCellValue((String) obj);
        else if (obj instanceof Integer)
            cell.setCellValue((Integer) obj);
    }
}
try {
    FileOutputStream out = new FileOutputStream(new 
File("C:\\temp\\demo.xlsx"));
    workbook.write(out);
    out.close();
    System.out.println("success");
} catch (Exception e) {
    e.printStackTrace();
}

Apache POI is notably disagreeable with newer excel filetypes. Apache POI 明显不适合较新的 excel 文件类型。 Changing your file extension to .xls should work将文件扩展名更改为.xls应该可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我尝试从JAVA打开excel生成的文件时,仅当我在Excel中禁用“受保护的视图”时,我才能查看内容 - Whenever I try to open the excel generated file from JAVA,I am able to view contenets only if I disable Protected View in Excel 执行我的 java apache poi 程序后无法打开 Excel 文件,我正在使用文件 output stream - Unable to open Excel file after excecuting my java apache poi program and i am using file output stream 将excel转换为.csv文件时,我无法转换日期并将其保存在csv文件中? - while converting excel to the .csv file i am not able to converting date and save it in the csv file? 在JTextpane中打开文件时自动为Java关键字着色 - Automatically Coloring the Java Keywords when i am open a file in JTextpane 我正在尝试使用Java将MySql数据导出到文件中,但是我无法获取表头 - I am trying to export MySql data to a file using Java, but I am not able to get the table headers 我无法获取文件路径 - I am not able to get the file path 我无法将所有数据写入文件 - I am not able to write all data to a file Java:在Jar中打开xml-File。 无法打开 - Java: Open xml-File inside a Jar. Not able to open it java用excel打开csv文件 - java open csv file using excel 如何在Java中打开和保存Excel文件 - How to open and save excel file in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM