生活随笔
收集整理的這篇文章主要介紹了
Java POI 导入Excell
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
##Java POI 導入Excell
導入表格
public String toExcel(HttpServletRequest request, HttpServletResponse response,String phone,String time,String sendListid,Integer type) throws IOException {
Map<String,Object> map=new HashMap<>();
if(time!=null && !"".equals(time)){
String time1=time.substring(0,10);
String time2=time.substring(13,23);
map.put(“time1”,time1);
map.put(“time2”,time2);
}
map.put(“phone”,phone);
map.put(“sendListid”,sendListid);
map.put(“type”,type);
List<Map<String,Object>> frequencyList = frequenceService.findAll(map);
HSSFWorkbook wb = null;
//List<MenuTree> ZorganizeFunctionsList=organizationalFormatService.getOrganizeFunctions(organizationalFormatId);//創建第一行表頭String title="抽獎列表";//創建第二行表頭String[] title2={"序號","手機號","微信名","openId","金額","訂單編號","時間"};//創建Excel工作簿XSSFWorkbook workbook = new XSSFWorkbook();//創建一個工作表sheetXSSFSheet sheet = workbook.createSheet("sheet0");//設置單元格寬度sheet.setColumnWidth(1,25*256);sheet.setColumnWidth(2,25*256);sheet.setColumnWidth(3,25*256);sheet.setColumnWidth(4,20*256);sheet.setColumnWidth(5,30*256);sheet.setColumnWidth(6,30*256);//創建第一行XSSFRow row = sheet.createRow(0);XSSFCell cell=row.createCell(0);row.setHeight((short) 400);// 定義單元格為字符串類型cell.setCellType(HSSFCell.ENCODING_UTF_16);cell.setCellValue(title);//合并范圍,參數(起始行號<int firstRow>,終止行號<int lastRow>, 起始列號<int firstCol>,終止列號<int lastCol>).(從0開始算,0代表Excel的第一行/列)CellRangeAddress CellRange=new CellRangeAddress(0,0,0,4);sheet.addMergedRegion(CellRange);XSSFCellStyle cellStyle = workbook.createCellStyle();//指定單元格居中對齊cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定單元格垂直居中對齊cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//設置單元格字體XSSFFont font = workbook.createFont();font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);font.setFontName("宋體");font.setFontHeight((short) 400);cellStyle.setFont(font);//指定單元格為cellcell.setCellStyle(cellStyle);//創建第1行XSSFRow row2=sheet.createRow(1);XSSFCell cell2x=null;//插入第二行數據for (int i = 0; i < title2.length; i++) {cell2x= row2.createCell(i);cell2x.setCellValue(title2[i]);cell2x.setCellStyle(cellStyle);}int flay=2;for(Map<String,Object> frequency:frequencyList){XSSFRow row3=sheet.createRow(flay);//排行XSSFCell GCell=row3.createCell(0);GCell.setCellValue(flay-1);//手機XSSFCell ACell=row3.createCell(1);ACell.setCellValue(frequency.get("phone").toString());//微信名稱XSSFCell BCell=row3.createCell(2);BCell.setCellValue(frequency.get("weName").toString());//openIdXSSFCell CCell=row3.createCell(3);CCell.setCellValue(frequency.get("openId").toString());//金額XSSFCell DCell=row3.createCell(4);DCell.setCellValue(frequency.get("moneys").toString());//訂單編號XSSFCell ECell=row3.createCell(5);if (frequency.get("sendListid")!=null){ECell.setCellValue(frequency.get("sendListid").toString());}else {ECell.setCellValue("");}//時間XSSFCell FCell=row3.createCell(6);FCell.setCellValue(StringUtil.dateToString1((Date) frequency.get("createTime")));XSSFCellStyle cellStyle2 = workbook.createCellStyle();//指定單元格居中對齊cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定單元格垂直居中對齊cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//指定單元格為ACellFCell.setCellStyle(cellStyle2);ACell.setCellStyle(cellStyle2);BCell.setCellStyle(cellStyle2);CCell.setCellStyle(cellStyle2);DCell.setCellStyle(cellStyle2);ECell.setCellStyle(cellStyle2);FCell.setCellStyle(cellStyle2);GCell.setCellStyle(cellStyle2);flay+=1;}//String pathUrl=request.getSession().getServletContext().getRealPath("");//存放的路徑String path1 = ResourceBundleUtil.getString("system", "fileupload.path");// File oldFile = new File(pathUrl).getParentFile();String fileName2="抽獎列表";//獲取當前系統路徑分隔符//String s=File.separator;//String pathUrl2=pathUrl.substring(0,pathUrl.lastIndexOf(s));String message="file/"+ fileName2 + ".xls";File file=new File(path1,message);try {file.createNewFile();//打開文件輸出流FileOutputStream fos = FileUtils.openOutputStream(file);//將workbook寫入流workbook.write(fos);fos.flush();fos.close();} catch (IOException e) {e.printStackTrace();}// String fileName=pathUrl2+s+"file"+s+fileName2+".xls";String fileName=path1+"/"+message;String downloadFilename=fileName2+".xls";FileDownLoadUtil.downLoad(response, fileName, downloadFilename);return null;}
2.工具類
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/**
*
}
總結
以上是生活随笔為你收集整理的Java POI 导入Excell的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。