POI3.10讀取xlsx,並解析日期型別資料 Demo
阿新 • • 發佈:2019-02-11
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
package excel; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.collections.CollectionUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; 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 java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/5/4. */ public class Prize517 { public static void main(String[] args) throws IOException { List<String> a = new ArrayList<>(); List<Integer> result = readExcel2007(); } public static List<Integer> readExcel2007(){ List<Integer> result = new ArrayList<>(); try{ File excelFile = new File("C:\\Users\\Administrator\\Desktop\\517菜品.xlsx"); FileInputStream is = new FileInputStream(excelFile);// 獲取檔案輸入流 XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 建立Excel2003檔案物件 XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一個工作表,索引是0 int c = sheet.getLastRowNum(); System.out.println("rows="+c); // 開始迴圈遍歷行,表頭不處理,從1開始 XSSFCell cell; for (int i = 1; i <= c; i++) { XSSFRow row = sheet.getRow(i);// 獲取行物件 if (row == null) {// 如果為空,不處理 continue; } //菜名 cell = row.getCell(0); System.out.println(getStringCellValue(cell)); //份數 cell = row.getCell(1); System.out.println(getStringCellValue(cell)); //價值 cell = row.getCell(2); System.out.println(getStringCellValue(cell)); //使用規則 cell = row.getCell(3); System.out.println(getStringCellValue(cell)); //使用期限開始 cell = row.getCell(4); System.out.println(getStringCellValue(cell)); //使用期限結束 cell = row.getCell(5); System.out.println(getStringCellValue(cell)); //門店列表 cell = row.getCell(6); System.out.println(getStringCellValue(cell)); } }catch(Exception e){ e.printStackTrace(); } return result; } /** * 獲取單元格資料內容為字串型別的資料 * * @param cell Excel單元格 * @return String 單元格資料內容 */ private static String getStringCellValue(XSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if (XSSFDateUtil.isCellDateFormatted(cell)) { // 如果是date型別則 ,獲取該cell的date值 strCell = new SimpleDateFormat("yyyy-MM-dd").format(XSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } else { // 純數字 cell.setCellType(XSSFCell.CELL_TYPE_STRING); strCell = String.valueOf(cell.getStringCellValue()); } break; case XSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case XSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; } public static String httpGet(String url) throws IOException { HttpGet httpget = new HttpGet(url); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse response = httpClient.execute(httpget); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity); return result; } }