HSSFSheet导出excel
来源:互联网 发布:怎样注册农村淘宝网店 编辑:程序博客网 时间:2024/06/06 04:37
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet1");
sheet.setColumnWidth((short) 0,(short)7*256);
sheet.setColumnWidth((short) 1,(short)47*256);
sheet.setColumnWidth((short) 2,(short)12*256);
sheet.setColumnWidth((short) 3,(short)12.43*256);
sheet.setColumnWidth((short) 4,(short)22*256);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)4));
sheet.addMergedRegion(new Region(1,(short)0,1,(short)4));
sheet.addMergedRegion(new Region(2,(short)0,2,(short)4));
//打印设置
// HSSFPrintSetup ps = sheet.getPrintSetup();
// ps.setLandscape(false); //打印方向,true:横向,false:纵向
// ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张
sheet.setMargin(HSSFSheet.BottomMargin, 0.4); //页边距(下)
sheet.setMargin(HSSFSheet.LeftMargin, 0.4); //页边距(左)
sheet.setMargin(HSSFSheet.RightMargin, 0.4); //页边距(右)
sheet.setMargin(HSSFSheet.TopMargin, 0.4); //页边距(上)
sheet.getPrintSetup().setFooterMargin(0.32);
sheet.getPrintSetup().setHeaderMargin(0.32);
//单元格样式1
HSSFCellStyle style1 = wb.createCellStyle();
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
//style1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
HSSFFont font=wb.createFont();
font.setFontName("SimHei");
font.setFontHeightInPoints((short)20); //字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
style1.setFont(font);
//单元格样式2
HSSFCellStyle style2 = wb.createCellStyle();
style2.setWrapText(true);//设置自动换行
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)18);
style2.setFont(font);
//单元格样式3
HSSFCellStyle style3 = wb.createCellStyle();
style3.setWrapText(true);//设置自动换行
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style3.setFont(font);
//单元格样式4
HSSFCellStyle style4 = wb.createCellStyle();
style4.setWrapText(true);//设置自动换行
style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style4.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style4.setWrapText(true);
style4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style4.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style4.setBorderTop(HSSFCellStyle.BORDER_THIN);
style4.setBorderRight(HSSFCellStyle.BORDER_THIN);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
style4.setFont(font);
//单元格样式5
HSSFCellStyle style5 = wb.createCellStyle();
style5.setWrapText(true);//设置自动换行
style5.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style5.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)18);
style5.setFont(font);
//设置单元格样式6为第二行和第三行特别设置
HSSFCellStyle style6 = wb.createCellStyle();
style6.setWrapText(true);//设置自动换行
style6.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
style6.setFont(font);
HSSFRow row = sheet.createRow((int) 0);
row.setHeight((short)1200);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(result.get("subject").toString());
cell.setCellStyle(style1);
row = sheet.createRow((int) 1);
row.setHeight((short)1200);
cell = row.createCell((short) 0);
cell.setCellValue("定于"+result.get("hysj")+",在"+result.get("hydd")+"召开"+result.get("type")+"。");
cell.setCellStyle(style6);
row = sheet.createRow((int) 2);
row.setHeight((short)1200);
cell = row.createCell((short) 0);
cell.setCellValue("出席人员:"+result.get("cxry"));
cell.setCellStyle(style6);
row = sheet.createRow((int) 3);
cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell.setCellStyle(style3);
cell = row.createCell((short) 1);
cell.setCellValue("议题");
cell.setCellStyle(style3);
cell = row.createCell((short) 2);
cell.setCellValue("汇报部门");
cell.setCellStyle(style3);
cell = row.createCell((short) 3);
cell.setCellValue("汇报人");
cell.setCellStyle(style3);
cell = row.createCell((short) 4);
cell.setCellValue("相关部门");
cell.setCellStyle(style3);
int num=4;
for(Map<String,Object> _map:list){
row = sheet.createRow((int) num);
cell = row.createCell((short) 0);
cell.setCellValue(_map.get("row").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 1);
cell.setCellValue(_map.get("subject").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 2);
cell.setCellValue(_map.get("xzdw").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 3);
cell.setCellValue(_map.get("hbr").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 4);
cell.setCellValue(_map.get("xgbm").toString());
cell.setCellStyle(style4);
num++;
}
CellRangeAddress region1=new CellRangeAddress((short)num, (short)num,(short)3, (short)4);
sheet.addMergedRegion(region1);
row = sheet.createRow((int) num);
row.setHeight((short)1000);
cell = row.createCell(3);
cell.setCellValue(result.get("adddept").toString());
cell.setCellStyle(style2);
num++;
CellRangeAddress region2=new CellRangeAddress((short)num, (short)num,(short)3, (short)4);
sheet.addMergedRegion(region2);
row = sheet.createRow((int) num);
row.setHeight((short)1000);
cell = row.createCell((short) 3);
cell.setCellValue(result.get("addtime").toString());
cell.setCellStyle(style2);
OutputStream os = null;
BeanCtx.getResponse().reset(); // 清空输出流
os = BeanCtx.getResponse().getOutputStream(); // 取得输出流
BeanCtx.getResponse().setHeader("Content-disposition", "attachment; filename="
+ new String((result.get("subject")+".xls").getBytes("gb2312"), "ISO-8859-1")); // 设定输出文件头
BeanCtx.getResponse().setContentType("application/msexcel"); // 定义输出类型
os.flush();
wb.write(os);
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet1");
sheet.setColumnWidth((short) 0,(short)7*256);
sheet.setColumnWidth((short) 1,(short)47*256);
sheet.setColumnWidth((short) 2,(short)12*256);
sheet.setColumnWidth((short) 3,(short)12.43*256);
sheet.setColumnWidth((short) 4,(short)22*256);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)4));
sheet.addMergedRegion(new Region(1,(short)0,1,(short)4));
sheet.addMergedRegion(new Region(2,(short)0,2,(short)4));
//打印设置
// HSSFPrintSetup ps = sheet.getPrintSetup();
// ps.setLandscape(false); //打印方向,true:横向,false:纵向
// ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张
sheet.setMargin(HSSFSheet.BottomMargin, 0.4); //页边距(下)
sheet.setMargin(HSSFSheet.LeftMargin, 0.4); //页边距(左)
sheet.setMargin(HSSFSheet.RightMargin, 0.4); //页边距(右)
sheet.setMargin(HSSFSheet.TopMargin, 0.4); //页边距(上)
sheet.getPrintSetup().setFooterMargin(0.32);
sheet.getPrintSetup().setHeaderMargin(0.32);
//单元格样式1
HSSFCellStyle style1 = wb.createCellStyle();
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
//style1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
HSSFFont font=wb.createFont();
font.setFontName("SimHei");
font.setFontHeightInPoints((short)20); //字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
style1.setFont(font);
//单元格样式2
HSSFCellStyle style2 = wb.createCellStyle();
style2.setWrapText(true);//设置自动换行
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)18);
style2.setFont(font);
//单元格样式3
HSSFCellStyle style3 = wb.createCellStyle();
style3.setWrapText(true);//设置自动换行
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style3.setFont(font);
//单元格样式4
HSSFCellStyle style4 = wb.createCellStyle();
style4.setWrapText(true);//设置自动换行
style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style4.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style4.setWrapText(true);
style4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style4.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style4.setBorderTop(HSSFCellStyle.BORDER_THIN);
style4.setBorderRight(HSSFCellStyle.BORDER_THIN);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
style4.setFont(font);
//单元格样式5
HSSFCellStyle style5 = wb.createCellStyle();
style5.setWrapText(true);//设置自动换行
style5.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style5.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)18);
style5.setFont(font);
//设置单元格样式6为第二行和第三行特别设置
HSSFCellStyle style6 = wb.createCellStyle();
style6.setWrapText(true);//设置自动换行
style6.setAlignment(HSSFCellStyle.ALIGN_LEFT);
font=wb.createFont();
font.setFontName("FangSong");
font.setFontHeightInPoints((short)16);
style6.setFont(font);
HSSFRow row = sheet.createRow((int) 0);
row.setHeight((short)1200);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(result.get("subject").toString());
cell.setCellStyle(style1);
row = sheet.createRow((int) 1);
row.setHeight((short)1200);
cell = row.createCell((short) 0);
cell.setCellValue("定于"+result.get("hysj")+",在"+result.get("hydd")+"召开"+result.get("type")+"。");
cell.setCellStyle(style6);
row = sheet.createRow((int) 2);
row.setHeight((short)1200);
cell = row.createCell((short) 0);
cell.setCellValue("出席人员:"+result.get("cxry"));
cell.setCellStyle(style6);
row = sheet.createRow((int) 3);
cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell.setCellStyle(style3);
cell = row.createCell((short) 1);
cell.setCellValue("议题");
cell.setCellStyle(style3);
cell = row.createCell((short) 2);
cell.setCellValue("汇报部门");
cell.setCellStyle(style3);
cell = row.createCell((short) 3);
cell.setCellValue("汇报人");
cell.setCellStyle(style3);
cell = row.createCell((short) 4);
cell.setCellValue("相关部门");
cell.setCellStyle(style3);
int num=4;
for(Map<String,Object> _map:list){
row = sheet.createRow((int) num);
cell = row.createCell((short) 0);
cell.setCellValue(_map.get("row").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 1);
cell.setCellValue(_map.get("subject").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 2);
cell.setCellValue(_map.get("xzdw").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 3);
cell.setCellValue(_map.get("hbr").toString());
cell.setCellStyle(style4);
cell = row.createCell((short) 4);
cell.setCellValue(_map.get("xgbm").toString());
cell.setCellStyle(style4);
num++;
}
CellRangeAddress region1=new CellRangeAddress((short)num, (short)num,(short)3, (short)4);
sheet.addMergedRegion(region1);
row = sheet.createRow((int) num);
row.setHeight((short)1000);
cell = row.createCell(3);
cell.setCellValue(result.get("adddept").toString());
cell.setCellStyle(style2);
num++;
CellRangeAddress region2=new CellRangeAddress((short)num, (short)num,(short)3, (short)4);
sheet.addMergedRegion(region2);
row = sheet.createRow((int) num);
row.setHeight((short)1000);
cell = row.createCell((short) 3);
cell.setCellValue(result.get("addtime").toString());
cell.setCellStyle(style2);
OutputStream os = null;
BeanCtx.getResponse().reset(); // 清空输出流
os = BeanCtx.getResponse().getOutputStream(); // 取得输出流
BeanCtx.getResponse().setHeader("Content-disposition", "attachment; filename="
+ new String((result.get("subject")+".xls").getBytes("gb2312"), "ISO-8859-1")); // 设定输出文件头
BeanCtx.getResponse().setContentType("application/msexcel"); // 定义输出类型
os.flush();
wb.write(os);
阅读全文
0 0
- HSSFSheet导出excel
- 用HSSFSheet创建复杂的excel表格
- 用HSSFSheet创建复杂的excel表格
- 导出EXCEL
- Excel导出
- 导出Excel
- 导出EXCEL
- 导出Excel
- EXCEL导出
- 导出Excel
- 导出excel
- 导出excel
- EXCEL导出
- 导出excel
- 导出Excel
- Excel导出。
- 导出excel
- Excel 导出
- MByteToWChar WCharToMByte
- TensorFlow CIFAR-10数据集
- CSS设计美丽之横排页面(小作品)
- nginx 点播mp4方法
- RSA私钥和公钥文件格式 (pkcs#7, pkcs#8, pkcs#12, pem)
- HSSFSheet导出excel
- 二维数组
- VS中C++静态/动态库文件的编译调试与使用
- 页面滚动到特定位置
- 实验四 数据查询——简单查询 Sql Server数据库实验
- win10 如何安装jdk 如何解决javac不是内部命令或是外部命令
- 猜数字
- python学习资源汇总
- Django 之 (1)基本命令