Java 如何在android中使用ApachePOI在现有xls或xlsx文件上写入数据
我尝试使用ApachePOI将数据放入template.xls文件(必要时可以是xlsx)中,但我无法理解,该文件仍然没有更改。PrintStackTrace中未引发异常。你能给我一个工作代码吗?我读了这么多文档,所以请帮助我使用工作代码。谢谢 我的代码:Java 如何在android中使用ApachePOI在现有xls或xlsx文件上写入数据,java,android,apache,apache-poi,xls,Java,Android,Apache,Apache Poi,Xls,我尝试使用ApachePOI将数据放入template.xls文件(必要时可以是xlsx)中,但我无法理解,该文件仍然没有更改。PrintStackTrace中未引发异常。你能给我一个工作代码吗?我读了这么多文档,所以请帮助我使用工作代码。谢谢 我的代码: final Button but = (Button) findViewById(R.id.bk); but.setOnClickListener(new View.OnClickListener() {
final Button but = (Button) findViewById(R.id.bk);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
writeXLSFile(3, 3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
public static void writeXLSFile(int row, int col) throws IOException {
try {
FileInputStream file = new FileInputStream(Environment.getExternalStorageDirectory().toString() +"telegran/"+ "ex.xls");
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;
//Update the value of cell
cell = sheet.getRow(row).getCell(col);
cell.setCellValue("changed");
file.close();
FileOutputStream outFile =new FileOutputStream(new File(Environment.getExternalStorageDirectory().toString() +"telegran/"+ "ex.xls"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}尝试使用file.close();在工作簿之后写入(输出文件);outFile.close() 我认为这里的问题是,您试图获取一行和一个单元格,而这些行和单元格不是一开始就创建的。
在调用
cell=sheet.getRow(Row)之前,需要先创建一行Row
代码>类似
XSSFRow row=sheet.createRow(rowIndex)代码>
并创建一个单元格
,就像
Cell cell = row.createCell(cellIndex);
有关更多详细信息,请参见此单元格3,3已创建并具有值。无论如何谢谢你,但是没有其他想法吗?你试过XSSF吗?我只使用过一次ApachePOI,我给出的链接的例子效果很好。是的,我使用过,但都是一样的:(github上有一个为android优化的经过编辑的ApachePOI,但更糟糕的是,你有没有听说过任何应用程序使用ApachePOI编辑现有的excel文件?好吧,这听起来很傻,但我认为你在“telegran”之前漏掉了“/”FileOutputStream outFile=新的FileOutputStream(新文件(Environment.getExternalStorageDirectory().toString()+“telegran/”+“ex.xls”);应为“/telegran/”+“ex.xls”