Java教程:若依框架如何设置导入导出模板全局为文本格式(解决科学计数问题)
第一步:在ExcelUtil工具类中增加此方法/*** 创建默认列样式** @param wb 工作薄对象* @param column 列*/private void createDefaultColumnStyle(Workbook wb, int column){// 设置默认输入类型为文本CellStyle style = wb.createCellStyle();DataFormat e
·
第一步:在ExcelUtil工具类中增加此方法
/**
* 创建默认列样式
*
* @param wb 工作薄对象
* @param column 列
*/
private void createDefaultColumnStyle(Workbook wb, int column)
{
// 设置默认输入类型为文本
CellStyle style = wb.createCellStyle();
DataFormat excelFormat = wb.createDataFormat();
// 自动换行
style.setWrapText(true);
// 文本格式
style.setDataFormat(excelFormat.getFormat("@"));
sheet.setDefaultColumnStyle(column, style);
}
第二步:在ExcelUtil工具类exportExcel方法修改如下:
/**
* 对list数据源将其里面的数据导入到excel表单
*
* @return 结果
*/
public void exportExcel(HttpServletResponse response) throws IOException {
try {
// 取出一共有多少个sheet.
double sheetNo = Math.ceil(list.size() / sheetSize);
for (int index = 0; index <= sheetNo; index++) {
createSheet(sheetNo, index);
// 产生一行
Row row = sheet.createRow(0);
int column = 0, tempColumn = 0;
// 写入各个字段的列头名称
for (Object[] os : fields) {
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
// 设置列默认格式
this.createDefaultColumnStyle(wb, tempColumn++);
}
if (Type.EXPORT.equals(type)) {
fillExcelData(index, row);
addStatisticsRow();
}
}
// 这里为了计算wookbook文件的大小,引入了ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
wb.write(byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
response.setContentLength(byteArrayOutputStream.size());
response.getOutputStream().write(bytes);
} catch (Exception e) {
log.error("导出Excel异常{}", e.getMessage());
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (response.getOutputStream() != null) {
try {
response.getOutputStream().close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
完成
更多推荐
已为社区贡献1条内容
所有评论(0)