poi讀取模板並生成多個sheet
阿新 • • 發佈:2019-01-26
public void loadMainRequirePlan(HttpServletResponse response) throws IOException {
Map<String,Object> map=new HashMap<String,Object>();
//根據當前時間查詢出資料
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
String date=/*sdf.format(new Date());*/"2018-03-04";
String date1=date+" 00:00:00";
String date2=date+" 23:59:59";
map.put("date1", date1);
map.put("date2", date2);
map.put("laboratory", laboratory);
//查詢出該事件段的所有操作人員
List<VIEW_MakeWorkSheetNew> listConner=vmws.selectConnerName(map);
// 讀取模板,寫入資料,下載
// path是指欲下載的檔案的路徑。
File file = new File("C:\\Users\\謝楊華\\Desktop\\模板.xls");
// 讀取檔案寫入資料
InputStream io = new FileInputStream(file);
HSSFWorkbook wbook = new HSSFWorkbook(io);
HSSFSheet sheetModel = wbook.getSheetAt(0);//獲取到sheet模板
//隱藏Sheet
wbook.removeSheetAt(0);
for(int s=0;s<listConner.size();s++) {
HSSFSheet sheet = wbook.createSheet(listConner.get(s).getConnerUsername()+"-"+sdf.format(new Date()));
copySheet(wbook, sheetModel, sheet, sheetModel.getFirstRowNum(), sheetModel.getLastRowNum());
int num = 0;
int rows = 0;
//根據不同的操作人員查詢出不同資料
map.put("connerName", listConner.get(s).getConnerUsername());
List<VIEW_MakeWorkSheetNew> list=vmws.reportExcel(map);
int a = list.size();
int max = sheet.getLastRowNum();
sheet.setDefaultRowHeightInPoints(30 * 20);
HSSFRow row = null;
HSSFCellStyle cellStyle = wbook.createCellStyle(); // 單元格樣式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
for (int i = 0; i < list.size(); i++) {
row = sheet.getRow(i + 1);
row = sheet.createRow(i + 1); // 該行無資料,建立行物件
for (int j = 0; j < 12; j++) {
row.createCell(j).setCellStyle(cellStyle);
}
row.getCell(0).setCellValue(list.get(i).getMpnum());
row.getCell(1).setCellValue(list.get(i).getCrnum());
row.getCell(2).setCellValue(list.get(i).getIoNum());
row.getCell(3).setCellValue(list.get(i).getStartTime());
row.getCell(4).setCellValue(list.get(i).getEcnend_date());
row.getCell(5).setCellValue(list.get(i).getProcedureName());
row.getCell(6).setCellValue(list.get(i).getAssignCount());
row.getCell(7).setCellValue(list.get(i).getEcnMachine());
row.getCell(8).setCellValue(list.get(i).getConnerUsername());
row.getCell(9).setCellValue(list.get(i).getBomName());
row.getCell(10).setCellValue(list.get(i).getProjectName());
row.getCell(11).setCellValue(list.get(i).getGyyq());
}
}
String name=sdf.format(new Date())+"已排計劃.xls";
// 取得檔名。
String filename = file.getName();
// 取得檔案的字尾名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
// 以流的形式下載檔案。
io = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[io.available()];
io.read(buffer);
io.close();
// 清空response
response.reset();
// 設定response的Header
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(name.getBytes(), "ISO-8859-1"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
wbook.write(toClient);
toClient.flush();
toClient.close();
}
/**
*
* @param Excel工作簿物件
* @param 模板Sheet頁
* @param 新建Sheet頁
* @param 模板頁的第一行
* @param 模板頁的最後一行
*/
private static void copySheet(HSSFWorkbook wb, HSSFSheet fromsheet, HSSFSheet newSheet, int firstrow, int lasttrow) {
// 複製一個單元格樣式到新建單元格
if ((firstrow == -1) || (lasttrow == -1) || lasttrow < firstrow) {
return;
}
HSSFRow fromRow = null;
HSSFRow newRow = null;
HSSFCell newCell = null;
HSSFCell fromCell = null;
// 設定列寬
for (int i = firstrow; i < lasttrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow != null) {
for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
int colnum = fromsheet.getColumnWidth((short) j);
if (colnum > 100) {
newSheet.setColumnWidth((short) j, (short) colnum);
}
if (colnum == 0) {
newSheet.setColumnHidden((short) j, true);
} else {
newSheet.setColumnHidden((short) j, false);
}
}
break;
}
}
// 複製行並填充資料
for (int i = 0; i < lasttrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow == null) {
continue;
}
newRow = newSheet.createRow(i - firstrow);
newRow.setHeight(fromRow.getHeight());
for (int j = fromRow.getFirstCellNum(); j < fromRow.getPhysicalNumberOfCells(); j++) {
fromCell = fromRow.getCell((short) j);
if (fromCell == null) {
continue;
}
newCell = newRow.createCell((short) j);
newCell.setCellStyle(fromCell.getCellStyle());
int cType = fromCell.getCellType();
newCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_STRING:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
newCell.setCellValue(fromCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
newCell.setCellValue(fromCell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(fromCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
newCell.setCellValue(fromCell.getErrorCellValue());
break;
default:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
}
}
}
}
Map<String,Object> map=new HashMap<String,Object>();
//根據當前時間查詢出資料
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
String date=/*sdf.format(new Date());*/"2018-03-04";
String date1=date+" 00:00:00";
String date2=date+" 23:59:59";
map.put("date1", date1);
map.put("date2", date2);
map.put("laboratory", laboratory);
//查詢出該事件段的所有操作人員
List<VIEW_MakeWorkSheetNew> listConner=vmws.selectConnerName(map);
// 讀取模板,寫入資料,下載
// path是指欲下載的檔案的路徑。
File file = new File("C:\\Users\\謝楊華\\Desktop\\模板.xls");
// 讀取檔案寫入資料
InputStream io = new FileInputStream(file);
HSSFWorkbook wbook = new HSSFWorkbook(io);
HSSFSheet sheetModel = wbook.getSheetAt(0);//獲取到sheet模板
//隱藏Sheet
wbook.removeSheetAt(0);
for(int s=0;s<listConner.size();s++) {
HSSFSheet sheet = wbook.createSheet(listConner.get(s).getConnerUsername()+"-"+sdf.format(new Date()));
copySheet(wbook, sheetModel, sheet, sheetModel.getFirstRowNum(), sheetModel.getLastRowNum());
int num = 0;
int rows = 0;
//根據不同的操作人員查詢出不同資料
map.put("connerName", listConner.get(s).getConnerUsername());
List<VIEW_MakeWorkSheetNew> list=vmws.reportExcel(map);
int a = list.size();
int max = sheet.getLastRowNum();
sheet.setDefaultRowHeightInPoints(30 * 20);
HSSFRow row = null;
HSSFCellStyle cellStyle = wbook.createCellStyle(); // 單元格樣式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
for (int i = 0; i < list.size(); i++) {
row = sheet.getRow(i + 1);
row = sheet.createRow(i + 1); // 該行無資料,建立行物件
for (int j = 0; j < 12; j++) {
row.createCell(j).setCellStyle(cellStyle);
}
row.getCell(0).setCellValue(list.get(i).getMpnum());
row.getCell(1).setCellValue(list.get(i).getCrnum());
row.getCell(2).setCellValue(list.get(i).getIoNum());
row.getCell(3).setCellValue(list.get(i).getStartTime());
row.getCell(4).setCellValue(list.get(i).getEcnend_date());
row.getCell(5).setCellValue(list.get(i).getProcedureName());
row.getCell(6).setCellValue(list.get(i).getAssignCount());
row.getCell(7).setCellValue(list.get(i).getEcnMachine());
row.getCell(8).setCellValue(list.get(i).getConnerUsername());
row.getCell(9).setCellValue(list.get(i).getBomName());
row.getCell(10).setCellValue(list.get(i).getProjectName());
row.getCell(11).setCellValue(list.get(i).getGyyq());
}
}
String name=sdf.format(new Date())+"已排計劃.xls";
// 取得檔名。
String filename = file.getName();
// 取得檔案的字尾名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
// 以流的形式下載檔案。
io = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[io.available()];
io.read(buffer);
io.close();
// 清空response
response.reset();
// 設定response的Header
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(name.getBytes(), "ISO-8859-1"));
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
wbook.write(toClient);
toClient.flush();
toClient.close();
}
/**
*
* @param Excel工作簿物件
* @param 模板Sheet頁
* @param 新建Sheet頁
* @param 模板頁的第一行
* @param 模板頁的最後一行
*/
private static void copySheet(HSSFWorkbook wb, HSSFSheet fromsheet, HSSFSheet newSheet, int firstrow, int lasttrow) {
// 複製一個單元格樣式到新建單元格
if ((firstrow == -1) || (lasttrow == -1) || lasttrow < firstrow) {
return;
}
HSSFRow fromRow = null;
HSSFRow newRow = null;
HSSFCell newCell = null;
HSSFCell fromCell = null;
// 設定列寬
for (int i = firstrow; i < lasttrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow != null) {
for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
int colnum = fromsheet.getColumnWidth((short) j);
if (colnum > 100) {
newSheet.setColumnWidth((short) j, (short) colnum);
}
if (colnum == 0) {
newSheet.setColumnHidden((short) j, true);
} else {
newSheet.setColumnHidden((short) j, false);
}
}
break;
}
}
// 複製行並填充資料
for (int i = 0; i < lasttrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow == null) {
continue;
}
newRow = newSheet.createRow(i - firstrow);
newRow.setHeight(fromRow.getHeight());
for (int j = fromRow.getFirstCellNum(); j < fromRow.getPhysicalNumberOfCells(); j++) {
fromCell = fromRow.getCell((short) j);
if (fromCell == null) {
continue;
}
newCell = newRow.createCell((short) j);
newCell.setCellStyle(fromCell.getCellStyle());
int cType = fromCell.getCellType();
newCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_STRING:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
newCell.setCellValue(fromCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
newCell.setCellValue(fromCell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(fromCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
newCell.setCellValue(fromCell.getErrorCellValue());
break;
default:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
}
}
}
}