导出excel
程序员文章站
2024-03-20 14:13:16
...
package com.fh.util;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import com.fh.util.PageData;
import com.fh.util.Tools;
/**
* 导入到EXCEL
* 类名称:ObjectExcelView.java
* @author FH Q313596790
* @version 1.0
*/
public class ObjectExcel extends AbstractXlsView{
@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Date date = new Date();
String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
HSSFSheet sheet;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
HSSFWorkbook book = (HSSFWorkbook) workbook;
sheet = book.createSheet("sheet1");
List<String> titles = (List<String>) model.get("titles");
int len = titles.size();
HSSFCellStyle headerStyle = book.createCellStyle(); //标题样式
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont headerFont = book.createFont(); //标题字体
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short)11);
headerStyle.setFont(headerFont);
short height=25*20;
HSSFRow row = sheet.createRow(0);
for(int i=0; i<len; i++){ //设置标题
String title = titles.get(i);
row.setRowStyle(headerStyle);
row.createCell(i).setCellValue(title);
}
sheet.getRow(0).setHeight(height);
int count=1;
HSSFCellStyle contentStyle = book.createCellStyle(); //内容样式
contentStyle.setAlignment(HorizontalAlignment.CENTER);
Object o=model.get("one");
row.setRowStyle(contentStyle);
count+=1;
row = sheet.createRow(count);
row.createCell(1).setCellValue(o.toString());
List<PageData> varList = (List<PageData>) model.get("varList1");
int varCount = varList.size();
for(int i=0; i<varCount; i++){
PageData vpd = varList.get(i);
count+=1;
HSSFRow rows = sheet.createRow(count);
// for(int j=0;j<len;j++){
// String varstr = StringDate.doub(vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "");
// rows.setRowStyle(contentStyle);
// rows.createCell(j).setCellValue(varstr);
// }
for(int j=0;j<=len;j++) {
Object object = vpd.get("var"+j);
if(object!=null) {
rows.setRowStyle(contentStyle);
rows.createCell(j).setCellValue(object.toString());
}
}
}
Object t=model.get("two");
row.setRowStyle(contentStyle);
count+=1;
row = sheet.createRow(count);
row.createCell(1).setCellValue(t.toString());
List<PageData> list = (List<PageData>) model.get("varList2");
int size = list.size();
for(int i=0; i<size; i++){
PageData vpd = list.get(i);
count+=1;
HSSFRow rows = sheet.createRow(count);
/* for(int j=0;j<len;j++){
String varstr = StringDate.doub(vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "");
rows.setRowStyle(contentStyle);
rows.createCell(j).setCellValue(varstr);
}*/
for(int j=0;j<=len;j++) {
Object object = vpd.get("var"+j);
if(object!=null) {
rows.setRowStyle(contentStyle);
rows.createCell(j).setCellValue(object.toString());
}
}
}
}
}
上一篇: servlet的Cookie学习
下一篇: c语言实现顺序表各个功能操作函数
推荐阅读
-
导出excel
-
EasyExcel导出excel使用教程
-
导出excel
-
java 使用EasyExcel导出Excel
-
掌财社:Java如何使用EasyExcel实现导入导出excel
-
java导出excel表格 使用alibaba easyexcel
-
EasyExcel使用ResponseEntity导出excel
-
java使用阿里的easyExcel实现Excel导出功能
-
Ireport,JAVA报表制作并导出(pdf、excel、word) 博客分类: IreportJAVA报表制作 ireport
-
birt 3.7.1导出PDF报表中文问题 博客分类: eclipse birt3.7.1PDF中文乱码