📄 deptexlservlet.java
字号:
package com.selectpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import com.login.UserInforBean;
public class DeptExlServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
Collection deptspart = (Collection) session.getAttribute("deptspart");
UserInforBean user=(UserInforBean)session.getAttribute("person");
// String outputFile = request.getRealPath("/excel/Excel.xls");
try {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel 工作簿中建一工作表
HSSFSheet sheet = workbook.createSheet("Sheet1");
// 设置行宽度
sheet.setColumnWidth((short) 0, (short) 4000);//
sheet.setColumnWidth((short) 1, (short) 8000);//
sheet.setColumnWidth((short) 2, (short) 8000);//
// 设置单元格格式(标题)
HSSFCellStyle styletitle = workbook.createCellStyle();
// 设置这些样式
styletitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont fonttitle = workbook.createFont();
fonttitle.setColor(HSSFColor.BLACK.index);
fonttitle.setFontHeightInPoints((short) 16);
fonttitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
styletitle.setFont(fonttitle);
// 设置单元格格式(文本)
HSSFCellStyle stylecontent = workbook.createCellStyle();
// 设置这些样式
//stylecontent.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//stylecontent.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
stylecontent.setBorderBottom(HSSFCellStyle.BORDER_THIN);
stylecontent.setBorderLeft(HSSFCellStyle.BORDER_THIN);
stylecontent.setBorderRight(HSSFCellStyle.BORDER_THIN);
stylecontent.setBorderTop(HSSFCellStyle.BORDER_THIN);
stylecontent.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// 生成一个字体
HSSFFont fontcontent = workbook.createFont();
fontcontent.setColor(HSSFColor.BLACK.index);
fontcontent.setFontHeightInPoints((short) 12);
// 把字体应用到当前的样式
stylecontent.setFont(fontcontent);
// 设置单元格格式(文本)
HSSFCellStyle stylecontent2 = workbook.createCellStyle();
// 设置这些样式
stylecontent.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// 生成一个字体
HSSFFont fontcontent2 = workbook.createFont();
fontcontent2.setColor(HSSFColor.BLACK.index);
fontcontent2.setFontHeightInPoints((short) 12);
// 把字体应用到当前的样式
stylecontent2.setFont(fontcontent2);
// 设置单元格格式(文本)
HSSFCellStyle stylecontent4 = workbook.createCellStyle();
// 设置这些样式
stylecontent.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// 把字体应用到当前的样式
stylecontent4.setFont(fontcontent2);
// 设置单元格格式(文本)
HSSFCellStyle stylecontent3 = workbook.createCellStyle();
// 设置这些样式
//stylecontent.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//stylecontent.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
stylecontent3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
stylecontent3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
stylecontent3.setBorderRight(HSSFCellStyle.BORDER_THIN);
stylecontent3.setBorderTop(HSSFCellStyle.BORDER_THIN);
stylecontent3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
// 生成一个字体
HSSFFont fontcontent3 = workbook.createFont();
fontcontent3.setColor(HSSFColor.BLACK.index);
fontcontent3.setFontHeightInPoints((short) 12);
// 把字体应用到当前的样式
stylecontent3.setFont(fontcontent3);
//合并单元格
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));
sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) 2));
// 在索引0的位置创建行(第一行)
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell1 = row.createCell((short) 0);// 第一列
// 定义单元格为字符串类型
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入数据
cell1.setCellValue("电力研究院部所绩效考核表");
cell1.setCellStyle(styletitle);
row = sheet.createRow((short) 2);
cell1 = row.createCell((short) 0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("部所");
cell1.setCellStyle(stylecontent2);
cell1 = row.createCell((short) 1);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("考核分(0至100分)");
cell1.setCellStyle(stylecontent2);
cell1 = row.createCell((short) 2);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("加减分(-10至+10分)");
cell1.setCellStyle(stylecontent2);
Iterator it = deptspart.iterator();
int i = 3;
String data="";
while (it.hasNext()) {
DeptPointBean dept = (DeptPointBean) it.next();
row = sheet.createRow((short) i);
cell1 = row.createCell((short) 0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue(dept.dept_name);
cell1.setCellStyle(stylecontent);
cell1 = row.createCell((short) 1);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue(dept.asses_point);
cell1.setCellStyle(stylecontent3);
cell1 = row.createCell((short) 2);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue(dept.ss_point);
cell1.setCellStyle(stylecontent3);
data=this.dateToString(dept.asses_date);
i++;
}
row = sheet.createRow((short) 1);
cell1 = row.createCell((short) 0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue(data);
cell1.setCellStyle(stylecontent4);
sheet.addMergedRegion(new Region(i, (short) 0, i, (short) 2));
row = sheet.createRow((short) i);
cell1 = row.createCell((short) 0);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("考核部门:"+user.dept+" 考核人:"+user.person_name);
cell1.setCellStyle(stylecontent4);
// 删除路径下同名的Excel 文件
// File path = new File(outputFile);
// path.delete();
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ new String("/Excel.xls".getBytes(), "iso-8859-1"));// 设定输出文件头
response.setContentType("application/msexcel");
// 新建一输出文件流
// FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(os);
// 操作结束,关闭文件
// fOut.flush();
// fOut.close();
// 该处如果Excel过大会影响效率,谁有好的想法可以提出来参考(不过从页面下载完后就会清空)
// request.getSession().setAttribute("Download", outputFile);
os.close();
} catch (Exception ioexception) {
request.setAttribute("message", "创建文件失败!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
}
public String dateToString(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM");
String ctime = formatter.format(time);
return ctime;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -