Excel的上传下载
生活随笔
收集整理的這篇文章主要介紹了
Excel的上传下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天我們來看Excel文件的上傳下載,這里我們使用到了Apache POI,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。
poi的結構
HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能。
XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能。
HWPF - 提供讀寫Microsoft Word DOC格式檔案的功能。
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。
HDGF - 提供讀Microsoft Visio格式檔案的功能。
HPBF - 提供讀Microsoft Publisher格式檔案的功能。
HSMF - 提供讀Microsoft Outlook格式檔案的功能。
HSSF與XSSF的區別
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,擴展名是.xls
XSSFWorkbook:是操作Excel2007的版本,擴展名是.xlsx
例子
首先要導入jar包,這里我們使用了maven來管理
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.8</version> </dependency>java代碼
package com.cloiot; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List;import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.cloiot.framework.util.DateUtils;public class ExcelUtils { public static void main(String[] args) { try { getExcelAsFile("d:/測試-201608171732.xlsx"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }String[] header = new String[]{"編號", "數值"};List<List<String>> list = new ArrayList<>();List<String> subList = new ArrayList<String>();subList.add("1");subList.add("20");list.add(subList);String fileExportName = "測試";ExcelUtils.exportExcel(header, list, fileExportName);} /** * 得到Excel,并解析內容 * @param file * @throws FileNotFoundException * @throws IOException */ public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{ XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file)); // 得到Excel工作簿對象XSSFSheet sheet = wb.getSheetAt(0); // 獲取第一個sheetint trLength = sheet.getLastRowNum(); //總行數 for(int i=0; i<=trLength; i++){ XSSFRow row = sheet.getRow(i); // 得到Excel工作表的行 int tdLength = row.getLastCellNum(); //總列數 for(int j=0; j<tdLength; j++){ XSSFCell cell = row.getCell(j); //得到Excel工作表指定行的單元格 /** * 為了處理:Excel異常Cannot get a text value from a numeric cell * 將所有列中的內容都設置成String類型格式 */ if(cell!=null){ cell.setCellType(Cell.CELL_TYPE_STRING); } //獲得每一列中的值 System.out.print(cell.getStringCellValue()+"\t\t\t"); } System.out.println(); } }/*** 下載 excel數據表* @param response* @param header:excel表頭* @param list:數據列表* @param fileExportName:下載文件名*/public static <E> void exportExcel(String[] header, List<List<String>> list, String fileExportName) {XSSFWorkbook wb = new XSSFWorkbook();//創建工作簿XSSFSheet sheet = wb.createSheet();//創建一個sheetXSSFRow headerRow = sheet.createRow(0);//創建一行XSSFRow contentRow = null;//設置標題for(int i=0;i<header.length;i++){headerRow.createCell(i).setCellValue(header[i]);}try {for(int i=0; i<list.size(); i++){contentRow = sheet.createRow(i+1);List<String> rowData = list.get(i);for (int j=0; j<rowData.size();j++) {contentRow.createCell(j).setCellValue(rowData.get(j));}}} catch (IllegalArgumentException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();}OutputStream os = null;SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.YYYYMMDD_12);String dateStr = "-" + formatter.format(new Date());String name = fileExportName+dateStr+".xlsx";try {os = new FileOutputStream(new File("D:/"+name));wb.write(os);os.flush();} catch (Exception e) {e.printStackTrace();} finally{if(os!=null){try {os.close();} catch (IOException e) {e.printStackTrace();}}}} }總結
以上是生活随笔為你收集整理的Excel的上传下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信公众号开发初探
- 下一篇: 2008年胡润中国富豪榜榜单(301-4