gpt4 book ai didi

java - 安卓出类拔萃。 jExecl 生成 0b 文件

转载 作者:行者123 更新时间:2023-11-30 03:53:47 27 4
gpt4 key购买 nike

我正在尝试使用 jExcel poi 在 Eclipse 中制作一个 excell 文件。一切似乎都很好,没有错误,它甚至生成了文件,但它们是 0b 大且无法打开。我设法从我的平板电脑将文件传输到 USB,我在其中运行我的代码。当我在我的 PC 上打开它时,它说:“您尝试打开的文件的格式与文件扩展名指定的格式不同. 在打开之前确认文件没有损坏并且来源可靠。”然后它问我是否要打开它。当我说"is"时,它会打开一个新的空白 excel 文件。不是我用java制作的文件。下面是我的代码:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
try {
WritableWorkbook wb = createWorkbook("Busotina "+n+".xls");
WritableSheet sheet = createSheet(wb, "Prvi",0);
writeCell(0, 0, "poz", true, sheet);
writeCell(2, 0, "poz", true, sheet);
writeCell(1, 1, "poz", true, sheet);
writeCell(2, 3, "poz", true, sheet);

} catch (Exception e) {
e.printStackTrace();
}
}

public WritableWorkbook createWorkbook(String fileName) throws WriteException{
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
File sd = Environment.getExternalStorageDirectory();
File dir = new File(sd.getAbsolutePath() + "/JExcelTest");
dir.mkdirs();
File wbfile = new File (dir, fileName);
WritableWorkbook wb = null;
try {
wb=Workbook.createWorkbook(wbfile, wbSettings);
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public WritableSheet createSheet(WritableWorkbook wb, String sheetName, int sheetIndex){
return wb.createSheet(sheetName,sheetIndex);
}
public void writeCell(int columnPosition, int rowPosition,String contents, boolean
headerCell, WritableSheet sheet) throws WriteException{
Label newCell = new Label (columnPosition,rowPosition,contents);
if(headerCell){
WritableFont headerFont = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
headerFormat.setAlignment(Alignment.CENTRE);
newCell.setCellFormat(headerFormat);
}
sheet.addCell(newCell);
}

所以,有没有人知道为什么它创建了目录,创建了想要的文件,但是文件有 0.00B 大,我无法打开或放入电子邮件附件或任何东西中。

提前致谢!

最佳答案

听起来你的文件输出流没有正确关闭(因此文件看起来是 0 字节并且不能被其他程序访问。

另一件奇怪的事情是您在进行更改之前关闭了工作簿。您应该只在插入内容后执行此操作。

您应该尝试像这样编写您的 createWorkbook 方法:

public WritableWorkbook createWorkbook(String fileName) throws WriteException{
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
File sd = Environment.getExternalStorageDirectory();
File dir = new File(sd.getAbsolutePath() + "/JExcelTest");
dir.mkdirs();
File wbfile = new File (dir, fileName);
WritableWorkbook wb = null;
try {
wb=Workbook.createWorkbook(wbfile, wbSettings);
wb.write();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(wb != null) wb.close();
}
return wb;
}

关于java - 安卓出类拔萃。 jExecl 生成 0b 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com