import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;
import com.om.check.model.MonthCheck;
import com.om.check.service.MonthCheckService;
@Controller("/printmanager")
public class PrintDispatchAction extends DispatchAction {
@Resource(name = "monthCheckService")
private MonthCheckService monthCheckService;
// 考核打印
public ActionForward printCheck(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setCharacterEncoding("utf-8");
String year = "";
String month = "";
String yearStr = request.getParameter("year");
if(!"".equals(yearStr))
{
year = yearStr;
}
String monthStr = request.getParameter("month");
if(!"".equals(monthStr))
{
month = monthStr;
}
//UUID uuid = UUID.randomUUID();
String fileName ="/check/"+year+"年"+month+"月"+"任务月度考核";
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
String path = request.getSession().getServletContext().getRealPath(fileName+".xls");
System.out.println(path);
createWorkbook(path, year, month,response.getOutputStream());
return null;
}
public void createWorkbook(String fileName, String year, String month,OutputStream output) {
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中创建一个名位FIRST的工作表
HSSFSheet sheet = workbook.createSheet("assessment");
// 设置表格的样式
HSSFCellStyle style0 = workbook.createCellStyle();
// 在索引0的位置创建行,对应着第一行
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
HSSFFont font0 = workbook.createFont();
// 写单元格的标题
// 指定列宽度
sheet.setColumnWidth((short) 0, (short) (15 * 256));
sheet.setColumnWidth((short) 1, (short) (20 * 256));
sheet.setColumnWidth((short) 2, (short) (20 * 256));
sheet.setColumnWidth((short) 3, (short) (20 * 256));
font0.setFontHeightInPoints((short) 10);
font0.setFontName("宋体");
// font0.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style0.setFont(font0);
style0.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style0.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style0.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style0.setBorderRight(HSSFCellStyle.BORDER_THIN);
style0.setBorderTop(HSSFCellStyle.BORDER_THIN);
cell.setCellStyle(style0);
cell.setCellValue("被考核人");
cell = row.createCell((short) 1);
cell.setCellStyle(style0);
cell.setCellValue("本月考核的任务数量");
cell = row.createCell((short) 2);
cell.setCellStyle(style0);
cell.setCellValue("月度考核总分数");
cell = row.createCell((short) 3);
cell.setCellStyle(style0);
cell.setCellValue("考核年月");
// TODO,从数据库中读取数据
List<MonthCheck> list = monthCheckService.findChecksByYearAndMonth(
year, month);
String date = year + "年" + month + "月";
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
MonthCheck monthCheck = (MonthCheck) list.get(i);
row = sheet.createRow((short) i + 1);
HSSFCell[] data = new HSSFCell[4];
for (short j = 0; j < 4; j++) {
// cell.setCellStyle(style0);
data[j] = row.createCell(j);
data[j].setCellStyle(style0);
}
data[0].setCellValue(monthCheck.getCheckMan());
data[1].setCellValue(monthCheck.getMissionNumer());
data[2].setCellValue(monthCheck.getMonthScores());
data[3].setCellValue(date);
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName);
workbook.write(output);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
POI报表
最新推荐文章于 2024-04-19 12:10:49 发布