`
terry0501
  • 浏览: 306506 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java导出Excel

 
阅读更多
写道
private void exportSelectSonProject(HttpServletRequest request, 
HttpServletResponse response) throws Exception { 
DoradoContext ctx = DoradoContext.getContext(); 
Map map = new HashMap<String, String>(); 

String flowProjectYear = toString(ctx.getParameter("flowProjectYear")); 
String flowStateId = toString(ctx.getParameter("flowStateId")); 
String flowProjectName = toString(ctx.getParameter("flowProjectName")); 
String flowProjectAcceptid = toString(ctx.getParameter("flowProjectAcceptid")); 
String flowProjectIndex = toString(ctx.getParameter("flowProjectIndex")); 
String flowProjectSn = toString(ctx.getParameter("flowProjectSn")); 
String sysOrgId = toString(ctx.getParameter("sysOrgId")); 
String flowProjectTypeid = toString(ctx.getParameter("flowProjectTypeid")); 
String flowProjectKindid = toString(ctx.getParameter("flowProjectKindid")); 
String flowProjectSequence = toString(ctx.getParameter("flowProjectSequence")); 
String flowProjectClassid = toString(ctx.getParameter("flowProjectClassid")); 
String startTime = toString(ctx.getParameter("startTime")); 
String endTime = toString(ctx.getParameter("endTime")); 

map.put("flowProjectYear", flowProjectYear); 
map.put("flowStateId", flowStateId); 
map.put("flowProjectName", flowProjectName); 
map.put("flowProjectAcceptid", flowProjectAcceptid); 
map.put("flowProjectIndex", flowProjectIndex); 
map.put("flowProjectSn", flowProjectSn); 
map.put("sysOrgId", sysOrgId); 
map.put("flowProjectTypeid", flowProjectTypeid); 
map.put("flowProjectKindid", flowProjectKindid); 
map.put("flowProjectSequence", flowProjectSequence); 
map.put("flowProjectClassid", flowProjectClassid); 
map.put("startTime", startTime); 
map.put("endTime", endTime); 

String reportName = "项目查询-IT项目预算汇总报表" + System.currentTimeMillis(); 
String sheetName = "IT项目预算汇总报表"; 
String fileName = reportName + ".xls";// 文件名 
response.setContentType("application/x-msdownload"); 
response.setHeader("Content-Disposition", "attachment; filename=" 
+ URLEncoder.encode(fileName, "UTF-8")); 
OutputStream os = response.getOutputStream();// 输出流 
CreativeExcelExport excel = new CreativeExcelExport(); // HSSFExcel对象 
String[] columnName = new String[] { "项目状态", "项目名称", "项目索引", "主办部门", "项目归属", 
"项目类型", "项目性质", "项目年度"}; 
List dataList = new ArrayList(); 
NewFlowProjectListAction act = (NewFlowProjectListAction) SpringBeanFactory 
.getBean("flowprojectAction_new"); 
FlowProjectSvc svc = (FlowProjectSvc) SpringBeanFactory 
.getBean("flowprojectSvc_new"); 
act.setFlowProjectSvc(svc); 
FlowProjectMgr mgr = (FlowProjectMgr) SpringBeanFactory 
.getBean("flowprojectMgr_new"); 
svc.setFlowProjectMgr(mgr); 
FlowProjectDao dao = (FlowProjectDao) SpringBeanFactory 
.getBean("flowprojectDao_new"); 
mgr.setFlowProjectDao(dao); 
dataList = svc.getSelectFlowProjectBaseInfos(map); 
excel.projectInfoDataToExcel(reportName, sheetName, os, dataList, columnName); 
} 



/** 
* 项目统计项目信息 
* 外部调用导入合并Excel接口的方法 
* 
* @param reportName 
* 报告标题 
* @param sheetName 
* 页名称 
* @param os 
* 输出流 
* @param data 
* 数据 
* @throws IOException 
* 
*/ 
@SuppressWarnings("unchecked") 
public void projectInfoDataToExcel(String reportName, String sheetName, 
OutputStream os, List data, String[] columnName) throws IOException { 
Sheet sheet = this.getHSSFSheet(sheetName); 
this.writeTitle(sheet, reportName, columnName.length); 
this.writeColumnName(sheet, columnName); 
exportProjectInfoData(sheet, data); 
writeResponse(os); 
} 



/** 
* 写文件标题 
* 
* @param sheet 
* @param titleName 
* 
*/ 
private void writeTitle(Sheet sheet, String titleName, int columnNum) { 
Row row = sheet.createRow((short) 0); 
// 设定标题位置 
CellStyle style = this.getWorkBook().createCellStyle(); 
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中 

style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
style.setBorderRight(HSSFCellStyle.BORDER_THIN); 
style.setBorderRight(HSSFCellStyle.BORDER_THIN); 
style.setWrapText(true); 

// 设置标题字体 
Font font = this.getWorkBook().createFont(); 
font.setFontHeightInPoints((short) 10); // 字体大小 
font.setFontName("Arial"); // 什么字体 
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗 
style.setFont(font); 
// Cell cell = null; 
// for (int i = 0; i < columnNum; i++) { 
// cell = row.createCell(i); 
// cell.setCellStyle(style); 
// if (i == 1 || i == 8 || i == 12 || i == 11 || i >= 14) { 
// 
// sheet.setColumnWidth(i, 10000); 
// } 
// if (i == 0 || i == 2 || (i >= 4 && i <= 7) || i == 9 || i == 13) { 
// sheet.setColumnWidth(i, 4000); 
// } else { 
// sheet.setColumnWidth(i, 2500); 
// } 
// } 
// cell.setCellValue(titleName); 
// this.MergeCell(sheet, 0, 0, 0, columnNum - 1); 

} 


/** 
* 写列名称 
* 
* @param sheet 
* @param titleName 
* 
*/ 
private void writeColumnName(Sheet sheet, String[] columnName) { 
Row row = sheet.createRow((short) 0); 
// 设定列位置 
CellStyle colNameStyle = this.getWorkBook().createCellStyle(); 
colNameStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 
colNameStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中 

colNameStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
colNameStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
colNameStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); 
colNameStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); 

// 设置列字体 
Font font = this.getWorkBook().createFont(); 
font.setFontHeightInPoints((short) 14); // 字体大小 
font.setFontName("Arial"); // 什么字体 
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗 
colNameStyle.setFont(font); 
for (int i = 0; i < columnName.length; i++) { 
Cell cell = row.createCell(i); 
cell.setCellValue(columnName[i]); 
cell.setCellStyle(colNameStyle); 
} 


} 
/** 
* 
* 负责将数据输出到Excel逻辑处理(单个报告) 
* @param sheet 
* @param dataList 
*/ 
@SuppressWarnings("unchecked") 
private void exportProjectInfoData(Sheet sheet, List dataList) { 
if (dataList != null && dataList.size() > 0) { 
for (int i = 0; i < dataList.size(); i++) { 
FlowProjectBaseinfoVO data = (FlowProjectBaseinfoVO) dataList 
.get(i); 
int curRow = i + 1; 
Row row = sheet.createRow((short) curRow); 
row.setHeight((short) 600); 

// 设定数据样式 
CellStyle style = this.getWorkBook().createCellStyle(); 
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中 

style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
style.setBorderRight(HSSFCellStyle.BORDER_THIN); 
style.setBorderTop(HSSFCellStyle.BORDER_THIN); 
style.setWrapText(true); 

Cell curCell0 = row.createCell(0); 
if (data.getFlowProjectSn()!= null) { 
curCell0.setCellValue(data.getFlowProjectSn()); 
} 
curCell0.setCellStyle(style); 
sheet.setColumnWidth(0, 8000); 

Cell curCell1 = row.createCell(1); 
if (data.getFlowProjectName() != null) { 
curCell1.setCellValue(data.getFlowProjectName()); 
} 
curCell1.setCellStyle(style); 
sheet.setColumnWidth(1, 4000); 

Cell curCell2 = row.createCell(2); 
if (data.getFlowProjectTypeName()!= null) { 
curCell2.setCellValue(data.getFlowProjectTypeName()); 
} 
curCell2.setCellStyle(style); 
sheet.setColumnWidth(2, 4000); 

Cell curCell3 = row.createCell(3); 
if (data.getFlowProjectKindName() != null) { 
curCell3.setCellValue(data.getFlowProjectKindName()); 
} 
curCell3.setCellStyle(style); 
sheet.setColumnWidth(3, 4000); 

Cell curCell4 = row.createCell(4); 
if (data.getFlowProjectClassName() != null) { 
curCell4.setCellValue(data.getFlowProjectClassName()); 
} 
curCell4.setCellStyle(style); 
sheet.setColumnWidth(4, 4000); 
Cell curCell5= row.createCell(5); 
if (data.getFlowProjectYear() != null) { 
curCell5.setCellValue(data.getFlowProjectYear()); 
} 
curCell5.setCellStyle(style); 
sheet.setColumnWidth(5, 4000); 
} 
} 
Row row = sheet.getRow(0); 
for(int i = 0; i < 16; i++){ 
if(i==0){ 
Cell cell = row.getCell(i); 
if(null != cell){ 
String str = cell.getStringCellValue(); 
int length = str.getBytes().length; 
sheet.setColumnWidth((short)i,(short)(length*600)); 
} 
}else{ 
Cell cell = row.getCell(i); 
if(null != cell){ 
String str = cell.getStringCellValue(); 
int length = str.getBytes().length; 
sheet.setColumnWidth((short)i,(short)(length*400)); 
} 
} 

} 
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics