// Excel 讀取寫入數據庫 // 3.8版本的poi 4.0 可以不用寫 parseCell 這個方法,可以直接賦值 STRING 類型
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import com.e6soft.base.service.JService; import com.e6soft.base.annotations.JCall; import com.e6soft.base.util.WebUtil; @JActionService(uri="ledger") public class Ledger extends JService { private String parseCell(Cell cell){ String cellStr = ""; switch(cell.getCellType()){ case HSSFCell.CELL_TYPE_NUMERIC : // 數學 if(HSSFDateUtil.isCellDateFormatted(cell)){ // 日期,時間 SimpleDateFormat sdf = null; if(cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")){ // 時間 HH:mm sdf = new SimpleDateFormat("HH:mm"); } else { sdf = new SimpleDateFormat("yyyy-MM-dd"); } cellStr = sdf.format(cell.getDateCellValue()); }else if(cell.getCellStyle().getDataFormat() == 58){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); double temp = cell.getNumericCellValue(); Date date = DateUtil.getJavaDate(temp); cellStr = sdf.format(date); }else{ double temp = cell.getNumericCellValue(); String style = cell.getCellStyle().getDataFormatString(); DecimalFormat format = new DecimalFormat(); if(style.equals("General")){ format.applyPattern("#"); } cellStr = format.format(temp); } break; case HSSFCell.CELL_TYPE_STRING : // 字符串 cellStr = cell.getRichStringCellValue().toString(); break; case HSSFCell.CELL_TYPE_BLANK : // 布爾 cellStr = ""; break; default : cellStr = ""; } return cellStr; } @JCall public void ImportTable() throws Exception{ // 讀取上傳文件的xls 存入數據庫 /*實現過程:用戶先上傳文件至服務器的臨時位置,然后寫一個讀取excel的方法,程序從服務器的臨時位置讀取excel文件; * 然后循環工作表和行,將單元格的內容存入集合;再上傳至臨時表(每次先清空),再根據sku(主鍵)更新正式數據庫即可完成導入文件更新字段信息。*/ String Id=WebUtil.getParam("id"); String fileSaverId=WebUtil.getParam("fileSaverId"); String fwqdz=WebUtil.getParam("dz"); String dqid = WebUtil.getParam("dqid"); // 文件id String gs=WebUtil.getParam("gs"); String sql = " SELECT I.FILE_INFO_ID,I.FILE_INDEX,I.FILE_FILENAME,I.FILE_URL,I.FILE_SAVER " +" FROM FSXC3.T_P_FILE_INFO I " +" WHERE I.FILE_SAVER = '"+fileSaverId+"' AND I.FILE_INDEX = '"+Id+"' and I.FILE_INFO_ID = '"+dqid+"'";// not_save_402881f66abf8173016abfae26d00011" List<Map<String, Object>> sj = dBSelect(sql); String wj = ""; String savePath=System.getProperty("catalina.home")+"/webapps/webdav/"; for(Map<String, Object> dz:sj){ wj = dz.get("file_info_id").toString(); } String widz = savePath+wj+".xls"; // 文件在服務器的地址 // 獲得文件所在地 File file = new File(widz); List<Map<String, Object>> qbsj = new ArrayList<Map<String,Object>>(); // 讀取文件 try { //同時支持Excel 2003、2007 //File excelFile = new File("F:\\MyEclipse 2017 CI\\SY\\src\\word\\aa.xls"); //創建文件對象 FileInputStream is = new FileInputStream(file); //文件流 Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的 int sheetCount = workbook.getNumberOfSheets(); //Sheet的數量 //遍歷每個Sheet //for (int s = 0; s < sheetCount; s++) { Sheet sheet = workbook.getSheetAt(0); int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數 //遍歷每一行 // 前3行不讀 for (int r = 2; r < rowCount; r++) { Row row = sheet.getRow(r); int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數 /* //遍歷每一個單元格 for (int c = 0; c < cellCount; c++) { Cell cell = row.getCell(c); // 序號 CellType cellType = cell.getCellType(); // STRING String cellValue = null; //在讀取單元格內容前,設置所有單元格中內容都是字符串類型 // cell.setCellType(CellType.STRING); // //設置單元格類型 cell.setCellType(CellType.STRING); // //設置單元格類型 //按照字符串類型讀取單元格內數據 cellValue = cell.getStringCellValue(); //在這里可以對每個單元格中的值進行二次操作轉化 System.out.print(cellValue + " "); } */ // 定義每一行的變量 //String ID = parseCell(row.getCell(0)); String XMMC = parseCell(row.getCell(0)); String ZTZ = parseCell(row.getCell(1)); String ZJLY = parseCell(row.getCell(2)); String GCJSTZ = parseCell(row.getCell(3)); String JSGM = parseCell(row.getCell(4)); String JHGQ = parseCell(row.getCell(5)); String C = parseCell(row.getCell(6)); String CC = parseCell(row.getCell(7)); String CCC = parseCell(row.getCell(8)); String CCCC = parseCell(row.getCell(9)); String XJ = parseCell(row.getCell(10)); String QQFY = parseCell(row.getCell(11)); String GCFY = parseCell(row.getCell(12)); String ZDCQFY = parseCell(row.getCell(13)); String TZXJ = parseCell(row.getCell(14)); String JDJH = parseCell(row.getCell(15)); String ZJJH = parseCell(row.getCell(16)); String LX = parseCell(row.getCell(17)); String WJJY = parseCell(row.getCell(18)); String ZCSS = parseCell(row.getCell(19)); String XMXZ = parseCell(row.getCell(20)); String CZWT = parseCell(row.getCell(21)); String YDJHCZ = parseCell(row.getCell(22)); String NF = parseCell(row.getCell(23)); //按照字符串類型讀取單元格內數據 //String a = A.getStringCellValue(); Map<String,Object> dataMap = new HashMap<String,Object>(); // 順序一致!! //dataMap.put("ID", ID);//ID ID dataMap.put("XMMC", XMMC); // XMMC dataMap.put("ZTZ", ZTZ); // ZTZ dataMap.put("ZJLY", ZJLY); // ZJLY dataMap.put("GCJSTZ", GCJSTZ); // GCJSTZ dataMap.put("JSGM", JSGM); // JSGM dataMap.put("JHGQ", JHGQ); // JHGQ dataMap.put("C", C); // C dataMap.put("CC", CC);// CC dataMap.put("CCC", CCC); // CCC dataMap.put("CCCC", CCCC); // CCCC dataMap.put("XJ", XJ); // XJ dataMap.put("QQFY", QQFY); // QQFY dataMap.put("GCFY", GCFY); // GCFY dataMap.put("ZDCQFY", ZDCQFY); // ZDCQFY dataMap.put("TZXJ", TZXJ); // TZXJ dataMap.put("JDJH", JDJH); // JDJH dataMap.put("ZJJH", ZJJH); // ZJJH dataMap.put("LX", LX); //LX dataMap.put("WJJY", WJJY); //WJJY dataMap.put("ZCSS", ZCSS); //ZCSS dataMap.put("XMXZ", XMXZ); //XMXZ dataMap.put("CZWT", CZWT); //CZWT dataMap.put("YDJHCZ", YDJHCZ); //YDJHCZ dataMap.put("NF", NF); //NF 年份,用於區分年度填報 //dataMap.put("DLRID", I); //DLRID 登錄人id dataMap.put("GS", gs); // gs // System.out.println(dataMap); // 加入 //qbsj.add(dataMap); // 寫入數據庫 this.insert(dataMap,"t_fsxc_tzjsjh",null); } // } } catch (Exception e) { e.printStackTrace(); } } }
