使用poi方式进行导出,可以进行多种设计
1.下载poi jar包
2.页面中制作一个按扭,其中代码如下
window.location.href = 'equipmentLedger.do?reqCode=exportExcel&temp_num='+temp_num+'&temp_head='+temp_head;
3. 在相应的java方法中写入如下代码
public ActionForward exportExcel(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
BaseActionForm aForm = (BaseActionForm) form;
Dto inDto = aForm.getParamAsDto(request);
String temp_num = inDto.getAsString("temp_num");// 二级标题序号
String[] temp_head = inDto.getAsString("temp_head").split(",");// 二级标题名
/**
* excel组织结构开始
*/
HSSFWorkbook wb = new HSSFWorkbook(); // 创建一个excel文件
HSSFSheet sheet = wb.createSheet("sheet1"); // 创建了一个工作簿
/***第一行样式start****/
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
//设置标题字体格式
HSSFFont font = wb.createFont();
//设置字体样式
font.setFontHeightInPoints((short)16); //--->设置字体大小
font.setFontName("Courier New"); // 设置字体,是什么类型例如:宋体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
style.setFont(font);
// style.setWrapText(true); // 是否自动换行
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderBottom((short)1);
style.setBorderTop((short)1);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
/***end****/
/***第二行样式start****/
HSSFCellStyle style2 = wb.createCellStyle(); // 样式对象
HSSFFont font2 = wb.createFont();
font2.setFontHeightInPoints((short)13); //--->设置字体大小
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直局中
style2.setBorderLeft((short)1);
style2.setBorderRight((short)1);
style2.setBorderBottom((short)1);
style2.setBorderTop((short)1);
style2.setFont(font2);
/***end****/
/***第三行样式start****/
HSSFCellStyle style3 = wb.createCellStyle(); // 样式对象
HSSFFont font3 = wb.createFont();
font3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直局中
style3.setBorderLeft((short)1);
style3.setBorderRight((short)1);
style3.setBorderBottom((short)1);
style3.setBorderTop((short)1);
style3.setFont(font3);
/***end****/
/** 表格第三行 start**/
HSSFRow row3 = sheet.createRow(2); // 创建一行
// 第三行第一列,第二行中进行合并,需要在第三行中把边框显示,否则边框显示不全
HSSFCell cell3_1 = row3.createCell((short)0); // 创建一个单元格
cell3_1.setCellStyle(style3);
// 所有列
for(int i = 1;i < temp_head.length; i++){
HSSFCell cell3_2 = row3.createCell((short)i); // 创建一个单元格
cell3_2.setCellValue(temp_head[i-1]);
cell3_2.setCellStyle(style3);
}
/** 表格第三行 end **/
/** 表格第二行 start**/
HSSFRow row2 = sheet.createRow(1); // 创建一行
// 创建第二行所有用到的格子,然后把边框设为有边框,合并单元格也必须创建,不创建则不会有边框
for(int i = 1;i < temp_head.length; i++){
HSSFCell cell = row2.createCell((short)i); // 创建一个单元格
cell.setCellStyle(style2);
}
// 跨两行表头
sheet.addMergedRegion(new Region(1, (short) 0, 2, (short)0));
HSSFCell cell2_0 = row2.createCell((short)0); // 创建一个单元格
cell2_0.setCellValue("序号"); // 缺省字段
cell2_0.setCellStyle(style3);
for(int i = 1;i < temp_head.length; i++) {
if(temp_head[i].equals("专用线单位名称")) {
sheet.addMergedRegion(new Region(1, (short) (i+1), 2, (short)(i+1)));
HSSFCell cell = row2.createCell((short)(i+1)); // 创建一个单元格
cell.setCellValue(temp_head[i]); // 缺省字段
cell.setCellStyle(style3);
}
if(temp_head[i].equals("接轨点")) {
sheet.addMergedRegion(new Region(1, (short) (i+1), 2, (short)(i+1)));
HSSFCell cell = row2.createCell((short)(i+1)); // 创建一个单元格
cell.setCellValue(temp_head[i]); // 缺省字段
cell.setCellStyle(style3);
}
}
// 判断股道名称是否存在
// 创建两个变量,一个用来记录开始点,一个用来记录数量
int startPoint = 0;
int endCount = 0;
for(int i = 1;i < temp_head.length; i++) {
if(temp_head[i].equals("股道名称")) {
// 如果查到,则记录开始
if(startPoint == 0) {
startPoint = i+1;
}
endCount ++;
} else if(temp_head[i].equals("股道长度")) {
if(startPoint == 0) {
startPoint = i+1;
}
endCount ++;
} else if(temp_head[i].equals("钢轨类型")) {
if(startPoint == 0) {
startPoint = i+1;
}
endCount ++;
} else if(temp_head[i].equals("专用线总延长公里")) {
if(startPoint == 0) {
startPoint = i+1;
}
endCount ++;
}
}
int count = startPoint + endCount - 1;
if(count > 0) {
// 四个参数分别是:起始行,起始列,结束行,结束列 (拥有长度减一)
sheet.addMergedRegion(new Region(1, (short) startPoint, 1, (short) (count)));
HSSFCell cell2_1 = row2.createCell((short)startPoint);
cell2_1.setCellValue("股道");
cell2_1.setCellStyle(style2);
}
int startPoint2 = 0;
int endCount2 = 0;
for(int i = 1;i < temp_head.length; i++) {
if(temp_head[i].equals("道岔编号")) {
// 如果查到,则记录开始
if(startPoint2 == 0) {
startPoint2 = i+1;
}
endCount2 ++;
} else if(temp_head[i].equals("左右开")) {
if(startPoint2 == 0) {
startPoint2 = i+1;
}
endCount2 ++;
} else if(temp_head[i].equals("岔枕类型")) {
if(startPoint2 == 0) {
startPoint2 = i+1;
}
endCount2 ++;
} else if(temp_head[i].equals("道岔型号")) {
if(startPoint2 == 0) {
startPoint2 = i+1;
}
endCount2 ++;
}
}
int count2 = startPoint2 + endCount2 - 1;
if(count2 > 0) {
// 四个参数分别是:起始行,起始列,结束行,结束列 (拥有长度减一)
sheet.addMergedRegion(new Region(1, (short) startPoint2, 1, (short) (count2)));
HSSFCell cell2_2 = row2.createCell((short)startPoint2);
cell2_2.setCellValue("道岔");
cell2_2.setCellStyle(style2);
}
/** 表格第二行 end**/
/** 表格第一行(标题)合并数据根据第三行数据而定 **/
HSSFRow row1 = sheet.createRow(0); // 创建一行
// 四个参数分别是:起始行,起始列,结束行,结束列 (拥有长度减一)
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) (temp_head.length-1)));
row1.setHeightInPoints(20); // 行高
HSSFCell cell1 = row1.createCell((short)0); // 创建一个单元格
cell1.setCellStyle(style);
cell1.setCellValue("专用线设备台账");
// 创建第一行所有用到的格子,然后把边框设为有边框
for(int i = 1;i < temp_head.length; i++){
HSSFCell cell = row1.createCell((short)i); // 创建一个单元格
cell.setCellStyle(style);
}
/** 表格第一行 end **/
// 算法结束
try{
response.reset();
response.setContentType("application/msexcel;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=\""
+ new String(("专用线设备台账" + ".xls").getBytes("GBK"),
"ISO8859_1") + "\"");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return mapping.findForward(null);
}
poi基本使用方式为:
一个excel表格:
HSSFWorkbook wb = new HSSFWorkbook(); |
一个工作表格(sheet):
HSSFSheet sheet = wb.createSheet("测试表格"); |
一行(row):
HSSFRow row1 = sheet.createRow(0); |
一个单元格(cell):
HSSFCell cell2 = row2.createCell((short)0) |
单元格格式(cellstyle):
HSSFCellStyle style4 = wb.createCellStyle() |
单元格内容格式()
HSSFDataFormat format= wb.createDataFormat(); |