java導出Excel(非模板)可導出多個sheet方式
更新時間:2024年09月25日 15:36:57 作者:小杰。
Java開發(fā)中,導出Excel是常見需求,有時需要支持多個Sheet導出,此技巧介紹非模板方式實現(xiàn)單標題單Sheet以及多Sheet導出,標題一致或不一致均可,可換成Map使用,適合個人開發(fā)者和需要Excel導出功能的場景
java導出Excel(非模板)可導出多個sheet
當初為了寫導出也是廢了不少勁,今天突然想起來就將此方法上傳,之前也寫過模板導出所以這個是非模板的。
方法
- 1.導出excel(單標題,單sheet)
- 2.含多個sheet 每個sheet中的標題一致
- 3.含多個sheet 每個sheet中的標題不一致
- 并且還有兩個其他導出格式。
注意事項
將PageData換成Map即可!?。?/p>
<!--依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId> org.apache.httpcomponents </groupId> <artifactId> httpclient </artifactId> <version>4.5.9</version> </dependency> <!--json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>net.sf.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0-RC-1</version> </dependency>
import net.sf.jxls.transformer.XLSTransformer; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; /** * @Auther: sjj * @Date: 2019/5/29 16:50 * @ClassName: ExcelUtil * @Description: 導出excel */ public class ExcelUtil { /** * 導出excel * @param pds 需要導出的數(shù)據(jù) * @param pd 導出excel公用數(shù)據(jù),需包含title(標題)、headers(逗號隔開,為導出表頭),fileName(文件名,只須寫上名字,這里會進行處理拼接) * @param request * @param response * @return */ public static void exportData(List<LinkedHashMap<Object,Object>> pds, PageData pd, HttpServletRequest request, HttpServletResponse response) { if(pds.size()<1){ // return StatusCode.IS_NULL; return; } try { HSSFWorkbook workbook = new HSSFWorkbook(); //設置標題 HSSFSheet sheet = workbook.createSheet(pd.getString("title")); //headers表示excel表中第一行的表頭 String[] headers=pd.getString("headers").split(","); //表頭字體 Font headerFont = workbook.createFont(); headerFont.setFontName("微軟雅黑"); headerFont.setColor(HSSFFont.COLOR_NORMAL); headerFont.setBold(true); //創(chuàng)建單元格,并設置值表頭 設置表頭居中 HSSFCellStyle styleMain = workbook.createCellStyle(); //水平居中 styleMain.setAlignment(HorizontalAlignment.CENTER); styleMain.setFont(headerFont); //導出樣式 HSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 自動換行 cellStyle_C.setWrapText(true); cellStyle_C.setAlignment(HorizontalAlignment.CENTER); //創(chuàng)建標題行 HSSFRow row = sheet.createRow(0); //在excel表中添加表頭 for(int i=0;i<headers.length;i++){ HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); cell.setCellStyle(styleMain); } int rowNum = 1; //遍歷集合數(shù)據(jù) for(LinkedHashMap<Object,Object> p :pds){ //依次創(chuàng)建行 HSSFRow row1 = sheet.createRow(rowNum); int i=0; //遍歷數(shù)據(jù) for (Object key: p.keySet()){ //創(chuàng)建行中列 并放入數(shù)據(jù) HSSFCell cell = row1.createCell(i); String value=String.valueOf(p.get(key)); cell.setCellValue(value!=null?value:""); if("null".equals(value)){ cell.setCellValue(""); } cell.setCellStyle(cellStyle_C); i++; } row1.setHeight((short) (26*10)); rowNum++; } //自動調(diào)整列寬 for (int i = 0;i <headers.length;i++){ if(i==headers.length-2){ sheet.setColumnWidth(i, 20 * 256); break; } if(i==3){ sheet.setColumnWidth(i, 20 * 256); continue; } sheet.autoSizeColumn(i, true); } //取得輸出流 OutputStream out = response.getOutputStream(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); //清空緩存 response.reset(); //設置相應內(nèi)容的編碼格式 response.setCharacterEncoding("UTF-8"); //1.導出名稱 String fileName =pd.getString("fileName")+dateFormat.format(new Date()); //文件名亂碼 //獲得瀏覽器信息并轉換為大寫 String agent = request.getHeader("User-Agent").toUpperCase(); //IE瀏覽器和Edge瀏覽器 if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { //其他瀏覽器 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定義輸出類型 response.setContentType("application/msexcel"); //保存Excel文件 workbook.write(out); //關閉文件流 out.close(); } catch (Exception e) { e.printStackTrace(); // return StatusCode.ERROR; } // return StatusCode.SUCCESS; } /** * 導出excel 含多個sheet 每個sheet中的標題一致 * @param pds 需要導出的數(shù)據(jù) * @param titles 導出excel公用數(shù)據(jù),需包含title(標題)、headers(逗號隔開,為導出表頭) * @param fileName (文件名,只須寫上名字,這里會進行處理拼接) * @param request * @param response * 注意 titles里面的數(shù)據(jù)順序要與pds中一致 不然導出時會出現(xiàn)標題與內(nèi)容不符 * @return */ public static void exportSheetData(Map<String,List<List<LinkedHashMap<Object,Object>>>> pds, List<PageData> titles, String fileName,HttpServletRequest request, HttpServletResponse response) throws IOException { if(pds.size()<1){ // return StatusCode.IS_NULL; return; } HSSFWorkbook workbook = new HSSFWorkbook(); //取得輸出流 OutputStream out = response.getOutputStream(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); //重命名 fileName+=dateFormat.format(new Date());; //清空緩存 response.reset(); //設置相應內(nèi)容的編碼格式 response.setCharacterEncoding("UTF-8"); //文件名亂碼 //獲得瀏覽器信息并轉換為大寫 String agent = request.getHeader("User-Agent").toUpperCase(); //IE瀏覽器和Edge瀏覽器 if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { //其他瀏覽器 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定義輸出類型 response.setContentType("application/msexcel"); int sheetNum=0; for(String nameKey:pds.keySet()){ List<List<LinkedHashMap<Object,Object>>> data=pds.get(nameKey); //設置標題 HSSFSheet sheet = workbook.createSheet(); //工地/消納場名作為sheet名 workbook.setSheetName(sheetNum,nameKey); //表頭字體 Font headerFont = workbook.createFont(); headerFont.setFontName("微軟雅黑"); headerFont.setColor(HSSFFont.COLOR_NORMAL); headerFont.setBold(true); //創(chuàng)建單元格,并設置值表頭 設置表頭居中 HSSFCellStyle styleMain = workbook.createCellStyle(); //水平居中 styleMain.setAlignment(HorizontalAlignment.CENTER); styleMain.setFont(headerFont); //導出樣式 HSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 自動換行 cellStyle_C.setWrapText(true); cellStyle_C.setAlignment(HorizontalAlignment.CENTER); int titleNum=0; int rowNum = 1; int startRow=0; for(List<LinkedHashMap<Object,Object>> list:data){ //放入數(shù)據(jù) //創(chuàng)建標題行 //主標題 HSSFRow title = null; //行標題 HSSFRow row = null; //為了避免標題對內(nèi)容進行覆蓋 if(rowNum==1){ title=sheet.createRow(0); row = sheet.createRow(rowNum); rowNum++; }else{ //讓標題與上一行內(nèi)容存在兩個空行 startRow=rowNum+2; title=sheet.createRow(startRow); row = sheet.createRow(rowNum+3); rowNum+=4; } PageData pd=titles.get(titleNum); //headers表示excel表中第一行的表頭 String[] headers=pd.getString("headers").split(","); //創(chuàng)建主標題 HSSFCell tieleCell = title.createCell(0); tieleCell.setCellValue(pd.getString("title")); tieleCell.setCellStyle(styleMain); for(int i=1;i<=headers.length;i++){ HSSFCell cell = title.createCell(i); // HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(""); cell.setCellStyle(styleMain); } // 合并日期占兩行(4個參數(shù),分別為起始行,結束行,起始列,結束列) // 行和列都是從0開始計數(shù),且起始結束都會合并 // 這里是合并excel中日期的兩行為一行 CellRangeAddress region = new CellRangeAddress(startRow, startRow, 0, headers.length-1); sheet.addMergedRegion(region); //主標題創(chuàng)建結束 //在excel表中添加表頭 for(int i=0;i<headers.length;i++){ HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); cell.setCellStyle(styleMain); } //表頭添加結束 //遍歷集合數(shù)據(jù) for(LinkedHashMap<Object,Object> p :list){ //依次創(chuàng)建行 HSSFRow row1 = sheet.createRow(rowNum); int i=0; //遍歷數(shù)據(jù) for (Object key: p.keySet()){ //創(chuàng)建行中列 并放入數(shù)據(jù) HSSFCell cell = row1.createCell(i); String value=String.valueOf(p.get(key)); cell.setCellValue(value!=null?value:""); if("null".equals(value)){ cell.setCellValue(""); } cell.setCellStyle(cellStyle_C); i++; } row1.setHeight((short) (26*10)); rowNum++; } //自動調(diào)整列寬 for (int i = 0;i <headers.length;i++){ if(i==headers.length-2){ sheet.setColumnWidth(i, 20 * 256); break; } if(i==3){ sheet.setColumnWidth(i, 20 * 256); continue; } sheet.autoSizeColumn(i, true); } titleNum++; // } } sheetNum++; } //保存Excel文件 workbook.write(out); //關閉文件流 out.close(); } /** * 導出excel 含多個sheet 每個sheet中的標題不一致 * @param pds 需要導出的數(shù)據(jù) * @param titles 導出excel公用數(shù)據(jù),需包含title(標題)、headers(逗號隔開,為導出表頭),可以List<PageData>不一致 * @param fileName (文件名,只須寫上名字,這里會進行處理拼接) * @param request * @param response * 注意 titles里面的數(shù)據(jù)順序要與pds中一致 不然導出時會出現(xiàn)標題與內(nèi)容不符 * @return */ public static void exportSheetTitle(LinkedHashMap<String,List<List<LinkedHashMap<Object,Object>>>> pds, LinkedList<List<PageData>> titles, String fileName,HttpServletRequest request, HttpServletResponse response) throws IOException { if(pds.size()<1){ // return StatusCode.IS_NULL; return; } HSSFWorkbook workbook = new HSSFWorkbook(); //取得輸出流 OutputStream out = response.getOutputStream(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); //重命名 fileName+=dateFormat.format(new Date());; //清空緩存 response.reset(); //設置相應內(nèi)容的編碼格式 response.setCharacterEncoding("UTF-8"); //文件名亂碼 //獲得瀏覽器信息并轉換為大寫 String agent = request.getHeader("User-Agent").toUpperCase(); //IE瀏覽器和Edge瀏覽器 if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { //其他瀏覽器 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定義輸出類型 response.setContentType("application/msexcel"); int sheetNum=0; for(String nameKey:pds.keySet()){ List<List<LinkedHashMap<Object,Object>>> data=pds.get(nameKey); //設置標題 HSSFSheet sheet = workbook.createSheet(); //工地/消納場名作為sheet名 workbook.setSheetName(sheetNum,nameKey); //表頭字體 Font headerFont = workbook.createFont(); headerFont.setFontName("微軟雅黑"); headerFont.setColor(HSSFFont.COLOR_NORMAL); headerFont.setBold(true); //創(chuàng)建單元格,并設置值表頭 設置表頭居中 HSSFCellStyle styleMain = workbook.createCellStyle(); //水平居中 styleMain.setAlignment(HorizontalAlignment.CENTER); styleMain.setFont(headerFont); //導出樣式 HSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 自動換行 cellStyle_C.setWrapText(true); cellStyle_C.setAlignment(HorizontalAlignment.CENTER); int titleNum=0; int rowNum = 1; int startRow=0; for(List<LinkedHashMap<Object,Object>> list:data){ //放入數(shù)據(jù) //創(chuàng)建標題行 //主標題 HSSFRow title = null; //行標題 HSSFRow row = null; //為了避免標題對內(nèi)容進行覆蓋 if(rowNum==1){ title=sheet.createRow(0); row = sheet.createRow(rowNum); rowNum++; }else{ //讓標題與上一行內(nèi)容存在兩個空行 startRow=rowNum+2; title=sheet.createRow(startRow); row = sheet.createRow(rowNum+3); rowNum+=4; } //遍歷每個sheet中的標題 for(PageData pd:titles.get(sheetNum)){ //headers表示excel表中第一行的表頭 String[] headers=pd.getString("headers").split(","); //創(chuàng)建主標題 HSSFCell tieleCell = title.createCell(0); tieleCell.setCellValue(pd.getString("title")); tieleCell.setCellStyle(styleMain); for(int i=1;i<=headers.length;i++){ HSSFCell cell = title.createCell(i); // HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(""); cell.setCellStyle(styleMain); } // 合并日期占兩行(4個參數(shù),分別為起始行,結束行,起始列,結束列) // 行和列都是從0開始計數(shù),且起始結束都會合并 // 這里是合并excel中日期的兩行為一行 CellRangeAddress region = new CellRangeAddress(startRow, startRow, 0, headers.length-1); sheet.addMergedRegion(region); //主標題創(chuàng)建結束 //在excel表中添加表頭 for(int i=0;i<headers.length;i++){ HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); cell.setCellStyle(styleMain); } //表頭添加結束 //遍歷集合數(shù)據(jù) for(LinkedHashMap<Object,Object> p :list){ //依次創(chuàng)建行 HSSFRow row1 = sheet.createRow(rowNum); int i=0; //遍歷數(shù)據(jù) for (Object key: p.keySet()){ //創(chuàng)建行中列 并放入數(shù)據(jù) HSSFCell cell = row1.createCell(i); String value=String.valueOf(p.get(key)); cell.setCellValue(value!=null?value:""); if("null".equals(value)){ cell.setCellValue(""); } cell.setCellStyle(cellStyle_C); i++; } row1.setHeight((short) (26*10)); rowNum++; } //自動調(diào)整列寬 for (int i = 0;i <headers.length;i++){ if(i==headers.length-2){ sheet.setColumnWidth(i, 20 * 256); break; } if(i==3){ sheet.setColumnWidth(i, 20 * 256); continue; } sheet.autoSizeColumn(i, true); } } titleNum++; // } } sheetNum++; } //保存Excel文件 workbook.write(out); //關閉文件流 out.close(); } /** * 導出excel * @qkp * * @param pds 需要導出的數(shù)據(jù) * @param pd 導出excel公用數(shù)據(jù),需包含templateFile(模板文件),fileName(文件名,只須寫上名字,這里會進行處理拼接) * @param mergeCells 合并單元格數(shù)組【firstRow,lastRow,firstCol,lastCol】 * @param request * @param response * @return */ public static void exportData(Map<Object,Object> pds, PageData pd,int []mergeCells, HttpServletRequest request, HttpServletResponse response) { if(pds.size()<1){ return; } try { XLSTransformer transformer = new XLSTransformer(); InputStream in = new FileInputStream(new File(pd.getString("templateFile"))); XSSFWorkbook workbook =(XSSFWorkbook)transformer.transformXLS(in, pds); Sheet sheet = workbook.getSheetAt(0); //合并單元格需要的邏輯 sheet.addMergedRegion(new CellRangeAddress(mergeCells[0], mergeCells[1], mergeCells[2], mergeCells[3]));//合并單元格的函數(shù) //表頭字體 Font headerFont = workbook.createFont(); headerFont.setFontName("微軟雅黑"); headerFont.setColor(HSSFFont.COLOR_NORMAL); headerFont.setBold(true); //創(chuàng)建單元格,并設置值表頭 設置表頭居中 //導出樣式 XSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 自動換行 cellStyle_C.setWrapText(true); cellStyle_C.setAlignment(HorizontalAlignment.CENTER); //取得輸出流 OutputStream out = response.getOutputStream(); //清空緩存 response.reset(); //設置相應內(nèi)容的編碼格式 response.setCharacterEncoding("UTF-8"); //1.導出名稱 String fileName =pd.getString("fileName"); //文件名亂碼 //獲得瀏覽器信息并轉換為大寫 String agent = request.getHeader("User-Agent").toUpperCase(); //IE瀏覽器和Edge瀏覽器 if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { //其他瀏覽器 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); //定義輸出類型 //response.setContentType("application/msexcel"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //保存Excel文件 workbook.write(out); //關閉文件流 out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 導出excel * @author qkp * @param pds 需要導出的數(shù)據(jù) * @param pd 導出excel公用數(shù)據(jù),需包含title(標題)、headers(逗號隔開,為導出表頭),fileName(文件名,只須寫上名字,這里會進行處理拼接) * @param request * @param response * @return */ public static void exportData2(List<LinkedHashMap<Object,Object>> pds, PageData pd, HttpServletRequest request, HttpServletResponse response) { if(pds.size()<1){ return; } try { HSSFWorkbook workbook = new HSSFWorkbook(); //設置標題 HSSFSheet sheet = workbook.createSheet(); //headers表示excel表中第一行的表頭 String[] headers=pd.getString("headers").split(","); //表頭字體 Font headerFont = workbook.createFont(); headerFont.setFontName("微軟雅黑"); headerFont.setColor(HSSFFont.COLOR_NORMAL); headerFont.setBold(true); //創(chuàng)建單元格,并設置值表頭 設置表頭居中 HSSFCellStyle styleMain = workbook.createCellStyle(); //水平居中 styleMain.setAlignment(HorizontalAlignment.CENTER); styleMain.setFont(headerFont); styleMain.setBorderBottom(BorderStyle.THIN); styleMain.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styleMain.setBorderTop(BorderStyle.THIN); styleMain.setTopBorderColor(IndexedColors.BLACK.getIndex()); styleMain.setBorderLeft(BorderStyle.THIN); styleMain.setLeftBorderColor(IndexedColors.BLACK.getIndex()); styleMain.setBorderRight(BorderStyle.THIN); styleMain.setRightBorderColor(IndexedColors.BLACK.getIndex()); //導出樣式 HSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 自動換行 cellStyle_C.setWrapText(true); cellStyle_C.setAlignment(HorizontalAlignment.CENTER); cellStyle_C.setBorderBottom(BorderStyle.THIN); cellStyle_C.setBottomBorderColor(IndexedColors.BLACK.getIndex()); cellStyle_C.setBorderTop(BorderStyle.THIN); cellStyle_C.setTopBorderColor(IndexedColors.BLACK.getIndex()); cellStyle_C.setBorderLeft(BorderStyle.THIN); cellStyle_C.setLeftBorderColor(IndexedColors.BLACK.getIndex()); cellStyle_C.setBorderRight(BorderStyle.THIN); cellStyle_C.setRightBorderColor(IndexedColors.BLACK.getIndex()); //創(chuàng)建列標題行 HSSFRow row = sheet.createRow(0); //標題行樣式 HSSFCellStyle titleStyle = workbook.createCellStyle(); //表頭字體 Font titleFont = workbook.createFont(); titleFont.setFontName("微軟雅黑"); titleFont.setColor(HSSFFont.COLOR_NORMAL); titleFont.setBold(true); titleFont.setFontHeightInPoints((short)18); titleStyle.setFont(titleFont); titleStyle.setAlignment(HorizontalAlignment.CENTER); HSSFCell titleCell =row.createCell(0); titleCell.setCellValue(pd.getString("title")); titleCell.setCellStyle(titleStyle); //合并單元格需要的邏輯 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,headers.length-1 ));//合并單元格的函數(shù) row.setRowStyle(titleStyle); row.setHeightInPoints(56); //列表題 row = sheet.createRow(1); row.setHeightInPoints(39); //在excel表中添加表頭 for(int i=0;i<headers.length;i++){ HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); cell.setCellStyle(styleMain); } int rowNum = 2; //遍歷集合數(shù)據(jù) for(LinkedHashMap<Object,Object> p :pds){ //依次創(chuàng)建行 HSSFRow row1 = sheet.createRow(rowNum); int i=0; //遍歷數(shù)據(jù) for (Object key: p.keySet()){ //創(chuàng)建行中列 并放入數(shù)據(jù) HSSFCell cell = row1.createCell(i); String value=String.valueOf(p.get(key)); cell.setCellValue(value!=null?value:""); if("null".equals(value)){ cell.setCellValue(""); } cell.setCellStyle(cellStyle_C); i++; } //row1.setHeight((short) (26*10)); rowNum++; } //自動調(diào)整列寬 for (int i = 0;i <headers.length;i++){ if(i==headers.length-2){ sheet.setColumnWidth(i, 20 * 256); break; } if(i==3){ sheet.setColumnWidth(i, 20 * 256); continue; } sheet.autoSizeColumn(i); //sheet.autoSizeColumn(i, true); } //取得輸出流 OutputStream out = response.getOutputStream(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); //清空緩存 response.reset(); //設置相應內(nèi)容的編碼格式 response.setCharacterEncoding("UTF-8"); //1.導出名稱 String fileName =pd.getString("fileName")+dateFormat.format(new Date()); //文件名亂碼 //獲得瀏覽器信息并轉換為大寫 String agent = request.getHeader("User-Agent").toUpperCase(); //IE瀏覽器和Edge瀏覽器 if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { //其他瀏覽器 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定義輸出類型 response.setContentType("application/msexcel"); //保存Excel文件 workbook.write(out); //關閉文件流 out.close(); } catch (Exception e) { e.printStackTrace(); // return StatusCode.ERROR; } // return StatusCode.SUCCESS; } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java+opencv3.2.0實現(xiàn)人臉檢測功能
這篇文章主要為大家詳細介紹了Java+opencv3.2.0實現(xiàn)人臉檢測功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02使用java代碼實現(xiàn)保留小數(shù)點的位數(shù)
因為個人應用的需要,所以就寫個簡單點的了。希望大家都給給建議,共同學習。需要的朋友也可以參考下2013-07-07Java根據(jù)坐標經(jīng)緯度計算兩點距離5種方法及校驗經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦
在項目開發(fā)過程中需要根據(jù)兩地經(jīng)緯度坐標計算兩地間距離,下面這篇文章主要給大家介紹了關于Java根據(jù)坐標經(jīng)緯度計算兩點距離5種方法以及校驗經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦,需要的朋友可以參考下2023-12-12idea插件之mybatis log plugin控制臺sql的問題
這篇文章主要介紹了idea插件之mybatis log plugin控制臺sql,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制2017-02-02Java通過MyBatis框架對MySQL數(shù)據(jù)進行增刪查改的基本方法
MyBatis框架由Java的JDBC API進一步封裝而來,在操作數(shù)據(jù)庫方面效果拔群,接下來我們就一起來看看Java通過MyBatis框架對MySQL數(shù)據(jù)進行增刪查改的基本方法:2016-06-06