Java Excel (POI) 【待完善】

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public void toExcelCollective(HttpServletRequest request,HttpServletResponse response) throws Exception{
//创建HSSFWorkbook对象(Excel的文本对象)
		HSSFWorkbook wb = new HSSFWorkbook();

		//建立新的sheet对象(Excel的表单),表名:成绩表
		HSSFSheet  sheet = wb.createSheet("成绩表");

		//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
		HSSFRow row1 = sheet.createRow(0);

		//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个)
		HSSFCell cell = row1.createCell(0);

		//设置单元格内容
		cell.setCellValue("学员成绩一览表");

		//合并单元格CellRangeAddress构造函数依次表示:起始行,截止行,起始列,截止列
		sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));

		//在sheet中创建第二行
		HSSFRow row2 = sheet.createRow(1);
		//创建单元格并设置单元格内容
		row2.createCell(0).setCellValue("姓名");
		row2.createCell(1).setCellValue("班级");
		row2.createCell(2).setCellValue("笔试成绩");
		row2.createCell(3).setCellValue("机试成绩");

		//在sheet中创建第三行
		HSSFRow row3 = sheet.createRow(2);
		row3.createCell(0).setCellValue("李小明");
		row3.createCell(1).setCellValue("小二班");
		row3.createCell(2).setCellValue("100");
		row3.createCell(3).setCellValue("100");
		
		try {
			OutputStream out = null;
			response.reset();
			//解决导出时文件名中文乱码问题
			response.setContentType("application/octet-stream;charset=utf-8");  
			response.setHeader("Content-Disposition", "attachment;filename = details.xls");
			
			
			out = response.getOutputStream();       
			wb.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
}

导出效果:

public void toExcelCollective(HttpServletRequest request,HttpServletResponse response) throws Exception{
	    HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件   
	    HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet   
	    sheet.createFreezePane(1, 3);// 冻结   
	    // 设置列宽   
	    sheet.setColumnWidth(0, 1000);   
	    sheet.setColumnWidth(1, 3500);   
	    sheet.setColumnWidth(2, 3500);   
	    sheet.setColumnWidth(3, 6500);   
	    sheet.setColumnWidth(4, 6500);   
	    sheet.setColumnWidth(5, 6500);   
	    sheet.setColumnWidth(6, 6500);   
	    sheet.setColumnWidth(7, 2500);   
	    // Sheet样式   
	    HSSFCellStyle sheetStyle = workbook.createCellStyle();   
	    // 背景色的设定   
	    sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);   
	    // 前景色的设定   
	    sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);   
	    // 填充模式   
	    sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);   
	    // 设置列的样式   
	    for (int i = 0; i <= 14; i++) {   
	      sheet.setDefaultColumnStyle((short) i, sheetStyle);   
	    }   
	    // 设置字体   
	    HSSFFont headfont = workbook.createFont();   
	    headfont.setFontName("黑体");   
	    headfont.setFontHeightInPoints((short) 22);// 字体大小   
	    headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗   
	    // 另一个样式   
	    HSSFCellStyle headstyle = workbook.createCellStyle();   
	    headstyle.setFont(headfont);   
	    headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
	    headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
	    headstyle.setLocked(true);   
	    headstyle.setWrapText(true);// 自动换行   
	    // 另一个字体样式   
	    HSSFFont columnHeadFont = workbook.createFont();   
	    columnHeadFont.setFontName("宋体");   
	    columnHeadFont.setFontHeightInPoints((short) 10);   
	    columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
	    // 列头的样式   
	    HSSFCellStyle columnHeadStyle = workbook.createCellStyle();   
	    columnHeadStyle.setFont(columnHeadFont);   
	    columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
	    columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
	    columnHeadStyle.setLocked(true);   
	    columnHeadStyle.setWrapText(true);   
	    columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色   
	    columnHeadStyle.setBorderLeft((short) 1);// 边框的大小   
	    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色   
	    columnHeadStyle.setBorderRight((short) 1);// 边框的大小   
	    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
	    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色   
	    // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)   
	    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);   
	  
	    HSSFFont font = workbook.createFont();   
	    font.setFontName("宋体");   
	    font.setFontHeightInPoints((short) 10);   
	    // 普通单元格样式   
	    HSSFCellStyle style = workbook.createCellStyle();   
	    style.setFont(font);   
	    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中   
	    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中   
	    style.setWrapText(true);   
	    style.setLeftBorderColor(HSSFColor.BLACK.index);   
	    style.setBorderLeft((short) 1);   
	    style.setRightBorderColor(HSSFColor.BLACK.index);   
	    style.setBorderRight((short) 1);   
	    style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
	    style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.   
	    style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.   
	    // 另一个样式   
	    HSSFCellStyle centerstyle = workbook.createCellStyle();   
	    centerstyle.setFont(font);   
	    centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
	    centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
	    centerstyle.setWrapText(true);   
	    centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);   
	    centerstyle.setBorderLeft((short) 1);   
	    centerstyle.setRightBorderColor(HSSFColor.BLACK.index);   
	    centerstyle.setBorderRight((short) 1);   
	    centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
	    centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.   
	    centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.   
	  
	    try {   
	      // 创建第一行   
	      HSSFRow row0 = sheet.createRow(0);   
	      // 设置行高   
	      row0.setHeight((short) 900);   
	      // 创建第一列   
	      HSSFCell cell0 = row0.createCell(0);   
	      cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));   
	      cell0.setCellStyle(headstyle);   
	      /**  
	       * 合并单元格  
	       *    第一个参数:第一个单元格的行数(从0开始)  
	       *    第二个参数:第二个单元格的行数(从0开始)  
	       *    第三个参数:第一个单元格的列数(从0开始)  
	       *    第四个参数:第二个单元格的列数(从0开始)  
	       */  
          //合并"中非发展基金投资项目调度会工作落实情况对照表"这一行
	      CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);   
	      sheet.addMergedRegion(range);   
	      // 创建第二行   
	      HSSFRow row1 = sheet.createRow(1);   
	      HSSFCell cell1 = row1.createCell(0);   
	      cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日       前次会议时间:2009年8月24日"));   
	      cell1.setCellStyle(centerstyle);   
	      // 合并单元格   
	      range = new CellRangeAddress(1, 2, 0, 7);   
	      sheet.addMergedRegion(range);   
	      // 第三行   
	      HSSFRow row2 = sheet.createRow(3);   
	      row2.setHeight((short) 750);   
	      HSSFCell cell = row2.createCell(0);   
	      cell.setCellValue(new HSSFRichTextString("责任者"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(1);   
	      cell.setCellValue(new HSSFRichTextString("成熟度排序"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(2);   
	      cell.setCellValue(new HSSFRichTextString("事项"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(3);   
	      cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(4);   
	      cell.setCellValue(new HSSFRichTextString("上周工作进展"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(5);   
	      cell.setCellValue(new HSSFRichTextString("本周工作计划"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(6);   
	      cell.setCellValue(new HSSFRichTextString("问题和建议"));   
	      cell.setCellStyle(columnHeadStyle);   
	      cell = row2.createCell(7);   
	      cell.setCellValue(new HSSFRichTextString("备 注"));   
	      cell.setCellStyle(columnHeadStyle);   
	     
	      // 列尾   
	      int footRownumber = sheet.getLastRowNum();   
	      HSSFRow footRow = sheet.createRow(footRownumber + 1);   
	      HSSFCell footRowcell = footRow.createCell(0);   
	      footRowcell.setCellValue(new HSSFRichTextString("  审  定:XXX   审  核:XXX    汇  总:XX"));   
	      footRowcell.setCellStyle(centerstyle);   
	      range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);   
	      sheet.addMergedRegion(range);   
	  
	      response.setContentType("application/ms-excel");   
	      response.setHeader("Content-disposition", "attachment;filename = 未命名.xls");   
	      OutputStream ouputStream = response.getOutputStream();   
	      workbook.write(ouputStream);   
	      ouputStream.flush();   
	      ouputStream.close();   
	  
	    } catch (Exception e) {   
	      e.printStackTrace();   
	    }   
	  }

导出效果:

 

【参考文章地址:】

https://blog.csdn.net/m0_37899388/article/details/72778348

https://www.cnblogs.com/pocketbook/articles/7736531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值