說明:
--------------------------------------------------------------------------------------------
從服務器上導出excel文件到本地
在一個excel工作簿中,有5個工單表,每個工作表有1萬行數據。
--------------------------------------------------------------------------------------------
使用的jar包在附件中。
效果如圖:
--------------------------------------------------------------------------------------------
從服務器上導出excel文件到本地
在一個excel工作簿中,有5個工單表,每個工作表有1萬行數據。
--------------------------------------------------------------------------------------------
使用的jar包在附件中。
效果如圖:

<%@ page language="java" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>導出excel</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> 導出excel <form action="excel" method="post"> <input type="submit" name="" value="導出"> </form> </body> </html>
<servlet> <servlet-name>outPutExcel</servlet-name> <servlet-class>output.OutputExcel</servlet-class> </servlet> <servlet-mapping> <servlet-name>outPutExcel</servlet-name> <url-pattern>/excel</url-pattern> </servlet-mapping>
package output; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; 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; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.Region; /** * EXCEL報表工具類. * * @author sun * @version */ public class ExportExcel { private HSSFWorkbook wb = null; private HSSFSheet sheet = null; /** * @param wb * @param sheet */ public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) { //super(); this.wb = wb; this.sheet = sheet; } /** * 創建通用EXCEL頭部 * * @param headString 頭部顯示的字符 * @param colSum 該報表的列數 */ public void createNormalHead(String headString, int colSum) { HSSFRow row = sheet.createRow(0); // 設置第一行 HSSFCell cell = row.createCell(0); //row.setHeight((short) 1000); // 定義單元格為字符串類型 cell.setCellType(HSSFCell.ENCODING_UTF_16);// 中文處理 cell.setCellValue(new HSSFRichTextString(headString)); // 指定合并區域 /** * public Region(int rowFrom, * short colFrom, * int rowTo, * short colTo) */ sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum)); //定義單元格格式,添加單元格表樣式,并添加到工作簿 HSSFCellStyle cellStyle = wb.createCellStyle(); //設置單元格水平對齊類型 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格居中對齊 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定單元格垂直居中對齊 cellStyle.setWrapText(true);// 指定單元格自動換行 // 設置單元格字體 HSSFFont font = wb.createFont(); //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //font.setFontName("宋體"); //font.setFontHeight((short) 600); //cellStyle.setFont(font); cell.setCellStyle(cellStyle); } /** * 創建通用報表第二行 * * @param params 統計條件數組 * @param colSum 需要合并到的列索引 */ public void createNormalTwoRow(String[] params, int colSum) { //創建第二行 HSSFRow row1 = sheet.createRow(1); row1.setHeight((short) 400); HSSFCell cell2 = row1.createCell(0); cell2.setCellType(HSSFCell.ENCODING_UTF_16); cell2.setCellValue(new HSSFRichTextString("時間:" + params[0] + "至" + params[1])); // 指定合并區域 /** * public Region(int rowFrom, short colFrom, int rowTo, short colTo) */ sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) colSum)); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格居中對齊 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定單元格垂直居中對齊 cellStyle.setWrapText(true);// 指定單元格自動換行 // 設置單元格字體 HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setFontHeight((short) 250); cellStyle.setFont(font); cell2.setCellStyle(cellStyle); } /** * 設置報表標題 * * @param columHeader 標題字符串數組 */ public void createColumHeader(String[] columHeader) { // 設置列頭 在第三行 HSSFRow row2 = sheet.createRow(2); // 指定行高 row2.setHeight((short) 600); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格居中對齊 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定單元格垂直居中對齊 cellStyle.setWrapText(true);// 指定單元格自動換行 // 單元格字體 HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setFontHeight((short) 250); cellStyle.setFont(font); /*cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單無格的邊框為粗體 cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色. cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setRightBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setTopBorderColor(HSSFColor.BLACK.index);*/ // 設置單元格背景色 cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFCell cell3 = null; for (int i = 0; i < columHeader.length; i++) { cell3 = row2.createCell(i); cell3.setCellType(HSSFCell.ENCODING_UTF_16); cell3.setCellStyle(cellStyle); cell3.setCellValue(new HSSFRichTextString(columHeader[i])); } } /** * 創建內容單元格 * * @param wb HSSFWorkbook * @param row HSSFRow * @param col short型的列索引 * @param align 對齊方式 * @param val 列值 */ public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col,short align, String val) { HSSFCell cell = row.createCell(col); cell.setCellType(HSSFCell.ENCODING_UTF_16); cell.setCellValue(new HSSFRichTextString(val)); HSSFCellStyle cellstyle = wb.createCellStyle(); cellstyle.setAlignment(align); cell.setCellStyle(cellstyle); } /** * 創建合計行 * @param colSum 需要合并到的列索引 * @param cellValue */ public void createLastSumRow(int colSum, String[] cellValue) { HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格居中對齊 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定單元格垂直居中對齊 cellStyle.setWrapText(true);// 指定單元格自動換行 // 單元格字體 HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setFontHeight((short) 250); cellStyle.setFont(font); //獲取工作表最后一行 HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1)); HSSFCell sumCell = lastRow.createCell(0); sumCell.setCellValue(new HSSFRichTextString("合計")); sumCell.setCellStyle(cellStyle); //合并 最后一行的第零列-最后一行的第一列 sheet.addMergedRegion(new Region(sheet.getLastRowNum(), (short) 0,sheet.getLastRowNum(), (short) colSum));// 指定合并區域 for (int i = 2; i < (cellValue.length + 2); i++) { //定義最后一行的第三列 sumCell = lastRow.createCell(i); sumCell.setCellStyle(cellStyle); //定義數組 從0開始。 sumCell.setCellValue(new HSSFRichTextString(cellValue[i-2])); } } /** * 輸入EXCEL文件 * * @param fileName 文件名 */ public void outputExcel(String fileName) { FileOutputStream fos = null; try { fos = new FileOutputStream(new File(fileName)); wb.write(fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //***************************************************** // set && get //***************************************************** /** * @return the sheet */ public HSSFSheet getSheet() { return sheet; } /** * @param sheet the sheet to set */ public void setSheet(HSSFSheet sheet) { this.sheet = sheet; } /** * @return the wb */ public HSSFWorkbook getWb() { return wb; } /** * @param wb the wb to set */ public void setWb(HSSFWorkbook wb) { this.wb = wb; } }
package output; public class Domain { private String one; private String two; private String three; private String four; private String five; private String six; private String seven; private String eight; private String nine; private String ten; public String getOne() { return one; } public void setOne(String one) { this.one = one; } public String getTwo() { return two; } public void setTwo(String two) { this.two = two; } public String getThree() { return three; } public void setThree(String three) { this.three = three; } public String getFour() { return four; } public void setFour(String four) { this.four = four; } public String getFive() { return five; } public void setFive(String five) { this.five = five; } public String getSix() { return six; } public void setSix(String six) { this.six = six; } public String getSeven() { return seven; } public void setSeven(String seven) { this.seven = seven; } public String getEight() { return eight; } public void setEight(String eight) { this.eight = eight; } public String getNine() { return nine; } public void setNine(String nine) { this.nine = nine; } public String getTen() { return ten; } public void setTen(String ten) { this.ten = ten; } }
package output; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; 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.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class OutputExcel extends HttpServlet{ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("helloworld"); List<Domain> list = new ArrayList<Domain>(); int max = 10000; String str = "測試長度"; for(int i=0; i<max; i++){ Domain domain = new Domain(); domain.setOne(str+"1"); domain.setTwo(str+"2"); domain.setThree(str+"3"); domain.setFour(str+"4"); domain.setFive(str+"5"); domain.setSix(str+"6"); domain.setSeven(str+"7"); domain.setEight(str+"8"); domain.setNine(str+"9"); domain.setTen(str+"10"); list.add(domain); } String fileName = "導出Excel.xls"; fileName = new String(fileName.getBytes("GBK"),"iso8859-1"); response.reset(); response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下載的文件名 response.setContentType("application/vnd.ms-excel"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); OutputStream output = response.getOutputStream(); BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output); //定義單元格報頭 String worksheetTitle = "Excel導出"; HSSFWorkbook wb = new HSSFWorkbook(); //創建列標頭LIST List<String> fialList = new ArrayList<String>(); fialList.add("列1"); fialList.add("列2"); fialList.add("列3"); fialList.add("列4"); fialList.add("列5"); fialList.add("列6"); fialList.add("列7"); fialList.add("列8"); fialList.add("列9"); fialList.add("列10"); // 計算該報表的列數 int number = fialList.size()-1; //================================================================== // 創建單元格樣式 HSSFCellStyle cellStyleTitle = wb.createCellStyle(); // 指定單元格居中對齊 cellStyleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格垂直居中對齊 cellStyleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定當單元格內容顯示不下時自動換行 cellStyleTitle.setWrapText(true); //------------------------------------------------------------------ HSSFCellStyle cellStyle = wb.createCellStyle(); // 指定單元格居中對齊 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格垂直居中對齊 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定當單元格內容顯示不下時自動換行 cellStyle.setWrapText(true); //------------------------------------------------------------------ // 設置單元格字體 HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setFontHeight((short) 200); cellStyleTitle.setFont(font); for(int z=0; z<5; z++){ //工作表名 String worksheet = "表"+(z+1); HSSFSheet sheet = wb.createSheet(worksheet); ExportExcel exportExcel = new ExportExcel(wb, sheet); // 創建報表頭部 exportExcel.createNormalHead(worksheetTitle, number); //定義第一行 HSSFRow row1 = sheet.createRow(1); HSSFCell cell1 = null; for(int i = 0; i < fialList.size(); i++) { cell1 = row1.createCell(i); cell1.setCellStyle(cellStyleTitle); cell1.setCellValue(new HSSFRichTextString(worksheet+fialList.get(i).toString())); } HSSFRow row = sheet.createRow(2); HSSFCell cell = row.createCell(1); Domain domain = new Domain(); for(int i=0; i<list.size(); i++){ domain = list.get(i); row = sheet.createRow(i+2); cell = row.createCell(0); cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getOne())); cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTwo())); cell = row.createCell(2); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getThree())); cell = row.createCell(3); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFour())); cell = row.createCell(4); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getFive())); cell = row.createCell(5); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSix())); cell = row.createCell(6); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getSeven())); cell = row.createCell(7); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getEight())); cell = row.createCell(8); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getNine())); cell = row.createCell(9); cell.setCellValue(new HSSFRichTextString(worksheet+domain.getTen())); } } try { bufferedOutPut.flush(); wb.write(bufferedOutPut); bufferedOutPut.close(); } catch (IOException e) { e.printStackTrace(); System.out.println( "Output is closed "); } finally { list.clear(); } } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
