话不多说,上代码:
ExcelUtils 工具类
代码语言:javascript复制package com.lvic.foss.web.utils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFillForegroundColor((short) 13);// 设置背景色
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
// 声明列对象
HSSFCell cell = null;
// 创建标题
for(int i=0;i<title.length;i ){
cell = row.createCell(i);
sheet.setColumnWidth(i, 4500);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
// 创建内容
for(int i=0;i<values.length;i ){
row = sheet.createRow(i 1);
for(int j=0;j<values[i].length;j ){
HSSFCell celli = row.createCell(j);
// 将内容按顺序赋给对应的列对象
celli.setCellValue(values[i][j]);
}
}
return wb;
}
}
代码语言:javascript复制 /**
* 发送响应流
* @param response
* @param fileName 文件名称
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/msexcel");
response.setHeader("Content-Disposition", "attachment;filename=" fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
核心数据处理代码:
代码语言:javascript复制 // 获取数据
List<ProjectTradeRequest> list = page.getResults();
// 标题
String[] title = {"项目所属团队","项目名称","项目等级","项目状态","所在团队","跟投人姓名","申请金额(元)","确认额度(元)","是否已打款", "已打款金额","未打款金额","申请状态","额度确认","打款确认","审核确认","申请时间"};
// 文件名称
String fileName = "flowDate" System.currentTimeMillis() ".xls";
// sheet名字
String sheetName = "跟投数据";
// 内容名字
String content [][] = new String [list.size()][title.length] ;
// 组装数据
for (int i = 0; i < list.size(); i ) {
ProjectTradeRequest obj = list.get(i);
if(i == (list.size()-1)){
DecimalFormat sf = new DecimalFormat("#.##");//保留四位小数
content[i][6] = sf.format(obj.getApplyAMT());
content[i][7] = sf.format(obj.getACKAMT());
content[i][9] = sf.format(obj.getPAYAMT());
content[i][10] = sf.format(obj.getACKAMT()-obj.getPAYAMT());
}else{
content[i][6] = String.valueOf(obj.getApplyAMT());
content[i][7] = String.valueOf(obj.getACKAMT());
content[i][9] = String.valueOf(obj.getPAYAMT());
content[i][10] = String.valueOf(obj.getACKAMT()-obj.getPAYAMT());
}
content[i][0] = obj.getProjectGroupName();
content[i][1] = obj.getProjectName();
content[i][2] = obj.getClevel();
content[i][3] = projectStatus(obj.getProjectStatus());
content[i][4] = obj.getProjectGroupNameUser();
content[i][5] = obj.getUserName();
content[i][8] = payStatus(obj.getTradeStatus() "");
content[i][11] = tradeStatus(obj.getTradeStatus() "");
content[i][12] = obj.getApplyACKUserName();
content[i][13] = obj.getPayACKUserName();
content[i][14] = obj.getFinalACKUserName();
content[i][15] = obj.getUpdatetimestamp();
}
// 生成excel
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
// 响应到客户端
try {
this.setResponseHeader(resp, fileName);
OutputStream os = resp.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
碰到问题:
科学计数法,
代码语言:javascript复制 DecimalFormat sf = new DecimalFormat("#.##");//保留四位小数
content[i][6] = sf.format(obj.getApplyAMT());