我有个简短的问题。我编写了生成jtable的程序,您可以用一些值填充这个表,而不是将整个表保存为excel。
在乞讨开始时,我定义了文件:
文件f=新文件(“C:\Users\Gregory\Desktop\test.xls”);
下面是导出JTable到excel的类:
公共空exportTable(JTable表,文件文件)抛出IOException { TableModel模型= table.getModel();FileWriter out =新FileWriter(文件); 对于(int i= 0;i< model.getColumnCount();i++) {model.getColumnCount+ \t");} out.write("\n");for (int i= 0;i< model.getRowCount();i++) { for (int j= 0;j< model.getColumnCount();j++) { if (i,j) = null) { out.write(model.getValueAt(i,j).toString() +“t”);} if (model.getValueAt(i,j) == null) {out.write(“t”);} out.write("\n");}out.close();
我不能将这个表导入到java程序中。我收到错误: jxl.read.biff.BiffException:无法识别OLE流
当我打开这个文件时,我会收到一条消息,即文件的格式与其扩展名(.xls)不同。所以我点击“另存为”,建议扩展为.txt。我将文件保存为.xls (似乎与java程序保存的文件相同),并且无法将JTable从该excel导入到java程序中,而不会出现任何错误。有人能建议如何用正确的扩展名保存文件吗?
发布于 2016-10-24 20:03:52
在参数 path中,传递生成的文件路径,在参数名称处传递所需的文件名。如果您感兴趣,则可以更改方法签名以动态接收扩展文件。只需替换参数的".xls"即可。
private void download(HttpServletRequest request, HttpServletResponse response, String path,
String name) throws IOException
{
OutputStream out = null;
try
{
out = response.getOutputStream();
}
catch (IOException e)
{
e.printStackTrace();
}
File arquivo = new File(path);
String sContentType = null;
sContentType = "application/pdf";
response.setContentType(sContentType);
response.addHeader("content-disposition", "attachment; filename=" + name + ".xls");
InputStream in = null;
in = new BufferedInputStream(new FileInputStream(arquivo));
if (in != null)
{
response.setContentLength((int) in.available());
int l;
byte b[] = new byte[4096];
while ((l = in.read(b, 0, b.length)) != -1)
{
out.write(b, 0, l);
}
out.flush();
in.close();
}
out.close();
}https://stackoverflow.com/questions/40226174
复制相似问题