JSP项目中,Struts2用的jxl.jar包,利用Excel模板导出Excel出错。
JSP项目中,Struts2用的jxl.jar包,利用Excel模板导出Excel,我没有写数据,打开导出的Excel后总是提示“文件错误。可能某些数字格式已丢失。"代码...
JSP项目中,Struts2用的jxl.jar包,利用Excel模板导出Excel,我没有写数据,打开导出的Excel后总是提示“文件错误。可能某些数字格式已丢失。"
代码:
<action name="exportTab1" method="exportTab1" class="xinstru.realname.web.actions.TabExportExcelAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;fileName=${fileName}</param>
<param name="bufferSize">4096</param>
</result>
</action>
public InputStream getInputStream() throws Exception {
File file = new File(fileName);
InputStream stream = new BufferedInputStream(new FileInputStream(file));
FileUtils.deleteQuietly(file);
return stream;
}
public String exportTab1() {
TabExportExcel tabExportExcel = new TabExportExcel();
fileName = tabExportExcel.exportTab(type, distName, list, startTime, endTime);
return SUCCESS;
}
package xinstru.web.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import xinstru.ejb.utilities.Utility;
import xinstru.web.xls.BaseExcel;
public class TabExportExcel extends BaseExcel {
public String exportTab(int type, String distName, List<Object[]> list, String startTime, String endTime) {
String path = "";
try {
// 模板excel文件
File sourceFile = new File(getTmpPath() + "/tab1.xls");
// 目标文件,在模板中填充数据并生成
path = getTmpPath() + "/" + getExcelName();
File targetFile = new File(path);
// 源文件读入
Workbook template = Workbook.getWorkbook(sourceFile);
if (targetFile.exists())
targetFile.delete();
// 目标文件先引入template中的内容,并将以targetFile导出
WritableWorkbook target = Workbook.createWorkbook(targetFile, template);
// 创建一个工作簿
WritableSheet ws = target.getSheet(0);
// 关闭文件
target.write();
target.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return path;
}
} 展开
代码:
<action name="exportTab1" method="exportTab1" class="xinstru.realname.web.actions.TabExportExcelAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;fileName=${fileName}</param>
<param name="bufferSize">4096</param>
</result>
</action>
public InputStream getInputStream() throws Exception {
File file = new File(fileName);
InputStream stream = new BufferedInputStream(new FileInputStream(file));
FileUtils.deleteQuietly(file);
return stream;
}
public String exportTab1() {
TabExportExcel tabExportExcel = new TabExportExcel();
fileName = tabExportExcel.exportTab(type, distName, list, startTime, endTime);
return SUCCESS;
}
package xinstru.web.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import xinstru.ejb.utilities.Utility;
import xinstru.web.xls.BaseExcel;
public class TabExportExcel extends BaseExcel {
public String exportTab(int type, String distName, List<Object[]> list, String startTime, String endTime) {
String path = "";
try {
// 模板excel文件
File sourceFile = new File(getTmpPath() + "/tab1.xls");
// 目标文件,在模板中填充数据并生成
path = getTmpPath() + "/" + getExcelName();
File targetFile = new File(path);
// 源文件读入
Workbook template = Workbook.getWorkbook(sourceFile);
if (targetFile.exists())
targetFile.delete();
// 目标文件先引入template中的内容,并将以targetFile导出
WritableWorkbook target = Workbook.createWorkbook(targetFile, template);
// 创建一个工作簿
WritableSheet ws = target.getSheet(0);
// 关闭文件
target.write();
target.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return path;
}
} 展开
展开全部
流的关闭放到finaly 里面去呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是不是到处的太多了?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询