package com.opensource.util.excel; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; 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.poi.hssf.util.HSSFColor; import org.apache.poi.ss.util.Region; import org.apache.struts.action.Action; public class CreateSimpleExcelToDisk extends Action { public static void ExportExcel(List<List> allList,String inWeek, HttpServletResponse response) throws Exception { // The first step, create a webbook, corresponding to an Excel file HSSFWorkbook wb = new HSSFWorkbook(); // The second step, add a sheet in the webbook, corresponding to the sheet in the Excel file HSSFSheet sheet = wb.createSheet(); // The third step, add the 0th row of the header to the sheet, note that the old version of poi has a limit on the number of rows and columns in Excel. HSSFRow rowT = sheet.createRow(0); rowT.setHeight((short)450); // The fourth step, create a cell, and set the value header to center the header //style table body use HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Create a centered format style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //style1 date week column is in use HSSFCellStyle style1 = wb.createCellStyle(); style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //title-specific style2 HSSFCellStyle style2 = wb.createCellStyle(); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // use style3 for column headers HSSFCellStyle style3 = wb.createCellStyle(); style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Create a centered format style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//Vertical center //Use style for the first week HSSFCellStyle style4 = wb.createCellStyle(); style4.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Create a centered format style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//Vertical center //add border style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //bottom border style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 // style.setBorderTop(HSSFCellStyle.BORDER_THIN);//Top border style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //bottom border style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//Top border style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); //bottom border style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style3.setBorderTop(HSSFCellStyle.BORDER_THIN);//Top border style3.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 /**font start*/ //generate a font HSSFFont font=wb.createFont(); font.setFontName("Imitation Song_GB2312"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //font color font.setFontHeightInPoints((short)12); //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold the font //Apply the font to the current style style.setFont(font); /**font end*/ /**font start*/ //Generate a font date for the week HSSFFont font1=wb.createFont(); font1.setFontName("Fake Song"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //font color font1.setFontHeightInPoints((short)12); font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold the font //Apply the font to the current style style1.setFont(font1); /**font end*/ /**font start*/ //Generate a font title with HSSFFont font2=wb.createFont(); font2.setFontName("Fake Song Dynasty"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //font color font2.setFontHeightInPoints((short)16); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold the font //Apply the font to the current style style2.setFont(font2); /**font end*/ /**font start*/ //Generate a font column header HSSFFont font3=wb.createFont(); font3.setFontName("Fake Song Dynasty"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //font color font3.setFontHeightInPoints((short)14); font3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold the font //Apply the font to the current style style3.setFont(font3); /**font end*/ /**font start*/ //Generate a font column header HSSFFont font4=wb.createFont(); font4.setFontName("Fake Song Dynasty"); //font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //font color font4.setFontHeightInPoints((short)11); // font4.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold the font //Apply the font to the current style style4.setFont(font4); /**font end*/ //Merge cells, the four parameters are: start row, start column, end row, end column sheet.addMergedRegion(new Region(0,(short)0,0,(short)5)); HSSFCell cellT = rowT.createCell((short)0); cellT.setCellValue("Company leadership week arrangement"); cellT.setCellStyle(style2); HSSFRow row2 = sheet.createRow((int) 1); row2.setHeight((short)435); sheet.addMergedRegion(new Region(1,(short)0,1,(short)5)); HSSFCell cell2 = row2.createCell((short)0);//The parameter is the first column of this row cell2.setCellValue(inWeek); cell2.setCellStyle(style4); HSSFRow row3 = sheet.createRow((int) 2); //Automatically adjust the column width // sheet.autoSizeColumn((short)0); //Adjust the width of the first column // sheet.autoSizeColumn((short)1); //Adjust the width of the second column // sheet.autoSizeColumn((short)2); //Adjust the width of the third column // sheet.autoSizeColumn((short)3); //Adjust the width of the fourth column // sheet.autoSizeColumn((short)4); //Adjust the width of the fifth column // sheet.autoSizeColumn((short)5); //Adjust the width of the sixth column HSSFCell cell = row3.createCell((short) 0); row3.setHeight((short)500); sheet.setColumnWidth((short) 0, (short) 3500);//Set the width cell.setCellValue("date"); cell.setCellStyle(style3); cell = row3.createCell((short) 1); cell.setCellValue("week"); cell.setCellStyle(style3); cell = row3.createCell((short) 2); sheet.setColumnWidth((short) 2, (short) 3500);//Set the width cell.setCellValue("time"); cell.setCellStyle(style3); cell = row3.createCell((short) 3); cell.setCellValue("地点"); cell.setCellStyle(style3); cell = row3.createCell((short) 4); sheet.setColumnWidth((short) 4, (short) 23000);//Set the width cell.setCellValue("Participate in leadership"); cell.setCellStyle(style3); cell = row3.createCell((short) 5); sheet.setColumnWidth((short) 5, (short) 5000);//Set the width cell.setCellValue("work items"); cell.setCellStyle(style3); // The fifth step, write the entity data. In the actual application, the data is obtained from the database, int lineNum = 3; for(List list : allList) { for (int j = 0; j < list.size(); j++) { sheet.addMergedRegion(new Region(lineNum,(short)0, lineNum+(list.size()-1),(short)0)); sheet.addMergedRegion(new Region(lineNum,(short)1, lineNum+(list.size()-1),(short)1)); row3 = sheet.createRow(lineNum+j); // get the object from the list row3.setHeight((short) 420); Map map = (Map)list.get(j); // The fourth step, create the cell and set the value HSSFCell cell4 =row3.createCell((short) 0); cell4.setCellValue(map.get("riqi").toString()); cell4.setCellStyle(style1); cell4=row3.createCell((short) 1); cell4.setCellValue(dayForWeek(map.get("riqi").toString())); cell4.setCellStyle(style1); cell4=row3.createCell((short) 2); cell4.setCellValue( map.get("shijian").toString()); cell4.setCellStyle(style); cell4= row3.createCell((short) 3); cell4.setCellValue( map.get("didian").toString()); cell4.setCellStyle(style); cell4=row3.createCell((short) 4); cell4.setCellValue( map.get("renyuan").toString()); cell4.setCellStyle(style); cell4=row3.createCell((short) 5); cell4.setCellValue(map.get("neirong").toString()); cell4.setCellStyle(style); } lineNum = lineNum + list.size(); } // Step 6, save the file to the specified location try { // FileOutputStream fout = new FileOutputStream("E:/students.xls"); // wb.write(fout); // fout.close(); ByteArrayOutputStream os = new ByteArrayOutputStream(); wb.write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Beijing Investment Company Leadership Week Work Arrangement"+inWeek+".xls") .getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } } catch (Exception e) { e.printStackTrace (); } } // Determine the day of the week based on the date public static String dayForWeek(String pTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); Calendar c = Calendar.getInstance(); c.setTime(format.parse(pTime)); int dayForWeek = 0; if (c.get(Calendar.DAY_OF_WEEK) == 1) { dayForWeek = 7; } else { dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } String week = "Monday"; switch(dayForWeek) { case 1: week = "Monday"; break; case 2: week = "Tuesday"; break; case 3: week = "Wednesday"; break; case 4: week = "Thursday"; break; case 5: week = "Friday"; break; case 6: week = "Saturday"; break; default: week = "Sunday"; break; } return week; } }
Export in excel form, java implementation code
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325951486&siteId=291194637
Recommended
Ranking