一、解析Word文檔表格數據
1.解析Word2007文檔表格數據(docx格式)
1 public static String getWordContent(String filePath) throws Exception{ 2 //定義解析文檔圖片存儲路徑 3 String storepicpath = filePath.substring(0, filePath.lastIndexOf("."));//獲取路徑最后以文件名命名的文件夾路徑 4 storepicpath = storepicpath.replaceAll("\\s*", "").replaceAll("\\.", "@@").replaceAll("\\#", "@@@").replaceAll("\\%", "@@@@");//去掉路徑中的空格 5 //開始解析文檔 6 FileInputStream fis = new FileInputStream(filePath); 7 //解析上傳引證商標明細文件 8 XWPFDocument xwpf = new XWPFDocument(fis);//得到word文檔的信息 9 Iterator<XWPFTable> it = xwpf.getTablesIterator();//迭代得到word中的表格 10 String content = "";//定義文檔表格內容 11 while(it.hasNext()){ 12 XWPFTable table = it.next();//獲取文檔中的表格 13 List<XWPFTableRow> rows=table.getRows();//獲取所有行數據 14 //讀取每一行數據 15 for (int i = 0; i < rows.size(); i++) { 16 if(i==4 || i==12){ 17 XWPFTableRow row = rows.get(i); 18 //讀取每一列數據 19 List<XWPFTableCell> cells = row.getTableCells(); 20 for (int j = 1; j < cells.size(); j++) { 21 XWPFTableCell cell=cells.get(j); 22 //輸出當前的單元格的數據 23 if(j == 2){ 24 //******************************開始解析表格中的商標圖樣圖片信息************************// 25 String picPath = ""; 26 List<XWPFParagraph> xwpfParagraphs = cell.getParagraphs(); 27 if(xwpfParagraphs == null) return null; 28 for(XWPFParagraph xwpfParagraph:xwpfParagraphs){ 29 List<XWPFRun> xwpfRunList = xwpfParagraph.getRuns(); 30 if(xwpfRunList==null) return null; 31 for(XWPFRun xwpfRun:xwpfRunList){ 32 List<XWPFPicture> xwpfPictureList = xwpfRun.getEmbeddedPictures(); 33 if(xwpfParagraph==null) return null; 34 for(XWPFPicture xwpfPicture:xwpfPictureList){ 35 xwpfPicture.getPictureData().getData(); 36 xwpfPicture.getPictureData().getFileName(); 37 storepicpath = storepicpath.replace("\\", "/"); 38 storepicpath = storepicpath.replaceAll("C:/Users/v-guoxiao/Desktop/Reference-Marks2", "D:/TradeMark/Tomcat8/webapps/ROOT/trademarkimage/ReferencePicture"); 39 File file = new File(storepicpath); 40 if(!file.exists()){ 41 file.mkdirs(); 42 } 43 picPath = storepicpath+"/"+ xwpfPicture.getPictureData().getFileName();//拼寫到存放數據的值 44 byte2image( xwpfPicture.getPictureData().getData(),storepicpath+"/"+ xwpfPicture.getPictureData().getFileName()); 45 } 46 } 47 } 48 content = content +"&@#"+ picPath; 49 }else{ 50 content = content +"&@#"+ cell.getText(); 51 } 52 } 53 }else { 54 XWPFTableRow row = rows.get(i); 55 //讀取每一列數據 56 List<XWPFTableCell> cells = row.getTableCells(); 57 for (int j = 1; j < cells.size(); j++) { 58 XWPFTableCell cell=cells.get(j); 59 //輸出當前的單元格的數據 60 content = content +"&@#"+ cell.getText(); 61 } 62 } 63 } 64 } 65 fis.close(); 66 return content; 67 } 68 69 /** 70 * 解析 DOCX 文檔中圖片工具方法 71 * @param data 72 * @param path 73 */ 74 public static void byte2image(byte[] data,String path){ 75 if(data.length<3||path.equals("")) return; 76 FileImageOutputStream imageOutput = null; 77 try{ 78 imageOutput = new FileImageOutputStream(new File(path)); 79 imageOutput.write(data, 0, data.length); 80 } catch(Exception ex) { 81 System.out.println("Exception: " + ex); 82 ex.printStackTrace(); 83 }finally { 84 try { 85 imageOutput.close(); 86 } catch (IOException e) { 87 e.printStackTrace(); 88 } 89 } 90 }
2.解析Word2003文檔表格數據(doc格式)
1 public static List<Map<String, Object>> getBigFotfmContent(String filePath) throws Exception{ 2 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 3 FileInputStream fis = new FileInputStream(filePath); 4 String storepath = filePath.substring(0, filePath.lastIndexOf("."));//獲取圖片存儲路徑 5 storepath = storepath.replaceAll("\\s*", "").replaceAll("\\.", "@@").replaceAll("\\#", "@@@").replaceAll("\\%", "@@@@"); 6 String content = "";//定義文檔表格內容 7 POIFSFileSystem pfs = new POIFSFileSystem(fis);//得到word文檔的信息 8 HWPFDocument hd = new HWPFDocument(pfs); 9 Range range = hd.getRange(); 10 TableIterator it = new TableIterator(range); 11 int p = 1; 12 while(it.hasNext()){ 13 Table table = it.next();//獲取表格 14 //讀取每一行數據 15 for (int i = 0; i < table.numRows(); i++) { 16 if(i == 0){ 17 TableRow row = table.getRow(i); 18 //讀取每一列數據 19 for (int j = 0; j < row.numCells(); j++) { 20 if(j == 1){ 21 TableCell cell = row.getCell(j); 22 PicturesTable pt = hd.getPicturesTable(); 23 CharacterRun cr = cell.getCharacterRun(0); 24 if(pt.hasPicture(cr)){ 25 Picture picture = pt.extractPicture(cr, true); 26 byte[] picbyte = picture.getContent(); 27 storepath = storepath.replace("\\", "/"); 28 storepath = storepath.replaceAll("C:/Users/v-guoxiao/Desktop/Reference-Marks2", "D:/TradeMark/Tomcat8/webapps/ROOT/trademarkimage/ReferencePicture"); 29 File file = new File(storepath); 30 if(!file.exists()){ 31 file.mkdirs(); 32 } 33 String picpath = storepath + "/image" + p + ".jpg"; 34 FileOutputStream fos = new FileOutputStream(picpath); 35 fos.write(picbyte); 36 fos.close(); 37 content = content + "&@#" + picpath; 38 } 39 }else{ 40 TableCell cell=row.getCell(j); 41 //輸出當前的單元格的數據 42 for(int k =0; k < cell.numParagraphs(); k++){ 43 Paragraph paragraph = cell.getParagraph(k); 44 String tradmark = paragraph.text().replaceAll("", ""); 45 if(!tradmark.equals("") && !tradmark.equals(null)){ 46 content = content + "&@#" + tradmark.replaceAll(":", ""); 47 } 48 } 49 } 50 } 51 }else { 52 TableRow row = table.getRow(i);//獲取行數據 53 //讀取每一列數據 54 for (int j = 0; j < row.numCells(); j++) { 55 TableCell cell=row.getCell(j);//獲取單元格數據 56 //輸出當前的單元格的數據 57 for(int k =0; k < cell.numParagraphs(); k++){ 58 Paragraph paragraph = cell.getParagraph(k); 59 String cellcontent = paragraph.text().replaceAll("", ""); 60 if(!cellcontent.equals("") && !cellcontent.equals(null)){ 61 content = content + "&@#" + cellcontent.replaceAll(":", ""); 62 } 63 } 64 } 65 } 66 } 67 p++; 68 } 69 //分割文檔內容,存儲到map集合中 70 String[] tables = content.split("Trademark"); 71 for(int i = 1; i < tables.length; i++){ 72 String data = "Trademark"+tables[i]; 73 data = data.replaceAll("\\s*", "").replaceAll("Trademark&@#&@#&@#", "Trademark&@#").replaceAll("Goods/Services&@#&@#", "Goods/Services&@#").replaceAll("Address&@#&@#", "Address&@#"); 74 String[] cellData = data.split("&@#",-1); 75 Map<String, Object> map =new HashMap<String, Object>(); 76 if(i < tables.length-1){ 77 if (cellData.length > 17) { 78 for(int m = 0; m < cellData.length-1; m++){ 79 if(m < 10 || m > 15){ 80 if(m%2 == 0 && m < cellData.length-1){ 81 map.put(cellData[m], cellData[m+1]); 82 } 83 }else if(m == 10 || m == 13){ 84 map.put(cellData[m], cellData[m+1] + cellData[m+2]); 85 } 86 } 87 }else{ 88 for(int m = 0; m < cellData.length-1; m++){ 89 if(m%2 == 0 && m < cellData.length-1){ 90 map.put(cellData[m], cellData[m+1]); 91 } 92 } 93 } 94 }else{ 95 if(cellData.length > 17){ 96 for(int m = 0; m < cellData.length; m++){ 97 if(m < 10 || m > 15){ 98 if(m%2 == 0 && m < cellData.length-1){ 99 map.put(cellData[m], cellData[m+1]); 100 } 101 }else if(m == 10 || m == 13){ 102 map.put(cellData[m], cellData[m+1] + cellData[m+2]); 103 } 104 } 105 }else{ 106 for(int m = 0; m < cellData.length; m++){ 107 if(m%2 == 0 && m < cellData.length-1){ 108 map.put(cellData[m], cellData[m+1]); 109 } 110 } 111 } 112 } 113 list.add(map); 114 } 115 return list; 116 }
3.解析Excel2003表格數據(xls格式)
1 public static String getDataFromExcelXls(String filePath) throws Exception{ 2 //獲取路徑中的Excel表的文件名稱 3 File file = new File(filePath.trim()); 4 String fileName = file.getName();//文件名稱 5 fileName = fileName.substring(0, fileName.lastIndexOf(".")).replaceAll("\\s*", ""); 6 String exContent = "";//定義接收循環表格的數據 7 FileInputStream fis = null;//定義輸出流 8 Workbook workbook = null;//定義工作簿 9 Sheet sheet = null;//定義sheet 10 //根據擴展名判斷解析Excel格式 11 fis = new FileInputStream(filePath); 12 workbook = new HSSFWorkbook(fis);//獲取工作簿 13 Map<String, PictureData> maplist = null; 14 //開始遍歷Excel表中多個sheet 15 for(int i = 0; i < workbook.getNumberOfSheets(); i++){//工作簿中多個sheet,循環遍歷出獲取每個sheet表中數據 16 sheet = workbook.getSheetAt(i);//獲取sheet表 17 String sheetName = sheet.getSheetName().replaceAll("\\s*", "").replaceAll("\\.", "");//獲取sheet名稱 18 maplist = getXlsPicture((HSSFSheet)sheet); 19 //取存圖片 20 try { 21 getExcelImage(maplist, fileName, sheetName); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 26 //開始解析表格數據 27 Date date;//定義所有日期 28 //獲取數據的總行數 29 int totalRowNum = sheet.getLastRowNum(); 30 //定義獲取的數據屬性名稱 31 String filingNum = "";//申請/注冊號 32 String type = "";//類型 33 String mark = "";//圖樣 34 String properter = "";//申請人 35 String filingDate = "";//申請日期 36 String startDate = "";//開始日期 37 String endDate = "";//結束日期 38 String status = "";//當前狀態 39 String isCancel= "";//是否取消 40 String goodsDescriptionEN = "";//商品英文描述 41 for(int m = 6; m <= totalRowNum; m++){ 42 Row row = sheet.getRow(m);//獲取第 m 行的數據 43 //獲取每列數據 44 Cell cell = row.getCell(0); 45 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請/注冊號 46 cell.setCellType(CellType.STRING); 47 filingNum = cell.getStringCellValue(); 48 exContent = exContent + "&@#filingNum&@#" + filingNum; 49 } 50 cell = row.getCell(1); 51 String picPath = "D:/TradeMark/Tomcat8/webapps/ROOT/trademarkimage/ReferencePicture/" + fileName + "/" + sheetName;//定義圖片存儲地址 52 if(cell.getCellTypeEnum() == CellType.BLANK){//****************************************圖片地址 53 cell.setCellType(CellType.STRING); 54 mark = picPath + "/" + (m+1) + "_" + 2 + ".jpg"; 55 exContent = exContent + "&@#mark&@#" + mark; 56 } 57 cell = row.getCell(2); 58 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請日期 59 if (cell.getCellTypeEnum() == CellType.STRING) {//判斷單元格內容的類型 60 cell.setCellType(CellType.STRING); 61 filingDate = cell.getStringCellValue(); 62 exContent = exContent + "&@#filingDate&@#" + filingDate; 63 }else{ 64 cell.setCellType(CellType.NUMERIC); 65 date = cell.getDateCellValue(); 66 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 67 filingDate = sdf.format(date); 68 exContent = exContent + "&@#filingDate&@#" + filingDate; 69 } 70 }else{ 71 cell.setCellType(CellType.STRING); 72 filingDate = cell.getStringCellValue(); 73 exContent = exContent + "&@#filingDate&@#" + filingDate; 74 } 75 cell = row.getCell(3); 76 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************注冊日期 77 if (cell.getCellTypeEnum() == CellType.STRING) { 78 cell.setCellType(CellType.STRING); 79 startDate = cell.getStringCellValue(); 80 exContent = exContent + "&@#startDate&@#" + startDate; 81 }else{ 82 cell.setCellType(CellType.NUMERIC); 83 date = cell.getDateCellValue(); 84 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 85 startDate = sdf.format(date); 86 exContent = exContent + "&@#startDate&@#" + startDate; 87 } 88 }else{ 89 cell.setCellType(CellType.STRING); 90 startDate = cell.getStringCellValue(); 91 exContent = exContent + "&@#startDate&@#" + startDate; 92 } 93 cell = row.getCell(4); 94 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************類型 95 cell.setCellType(CellType.STRING); 96 type = cell.getStringCellValue(); 97 exContent = exContent + "&@#type&@#" + type; 98 } 99 cell = row.getCell(5); 100 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************商品英文描述 101 cell.setCellType(CellType.STRING); 102 goodsDescriptionEN = cell.getStringCellValue(); 103 exContent = exContent + "&@#goodsDescriptionEN&@#" + goodsDescriptionEN; 104 }else{ 105 exContent = exContent + "&@#goodsDescriptionEN&@#" + goodsDescriptionEN; 106 } 107 cell = row.getCell(6); 108 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請人 109 cell.setCellType(CellType.STRING); 110 properter = cell.getStringCellValue(); 111 exContent = exContent + "&@#properter&@#" + properter; 112 } 113 cell = row.getCell(7); 114 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************注冊到期日 115 if (cell.getCellTypeEnum() == CellType.STRING) { 116 cell.setCellType(CellType.STRING); 117 endDate = cell.getStringCellValue(); 118 exContent = exContent + "&@#endDate&@#" + endDate; 119 }else{ 120 cell.setCellType(CellType.NUMERIC); 121 date = cell.getDateCellValue(); 122 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 123 endDate = sdf.format(date); 124 exContent = exContent + "&@#endDate&@#" + endDate; 125 } 126 }else{ 127 cell.setCellType(CellType.STRING); 128 endDate = cell.getStringCellValue(); 129 exContent = exContent + "&@#endDate&@#" + endDate; 130 } 131 cell = row.getCell(8); 132 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************當前狀態 133 cell.setCellType(CellType.STRING); 134 status = cell.getStringCellValue(); 135 exContent = exContent + "&@#status&@#" + status; 136 } 137 cell = row.getCell(9); 138 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************是否取消 139 cell.setCellType(CellType.STRING); 140 isCancel = cell.getStringCellValue(); 141 exContent = exContent + "&@#isCancel&@#" + isCancel; 142 } 143 } 144 } 145 return exContent; 146 }
4.解析Excel2007表格數據(xlsx格式)
1 public static String getDataFromExcelXlsx(String filePath) throws Exception{ 2 //獲取路徑中的Excel表的文件名稱 3 File file = new File(filePath.trim()); 4 String fileName = file.getName();//文件名稱 5 fileName = fileName.substring(0, fileName.lastIndexOf(".")).replaceAll("\\s*", ""); 6 String exContent = "";//定義接收循環表格的數據 7 FileInputStream fis = null;//定義輸出流 8 Workbook workbook = null;//定義工作簿 9 Sheet sheet = null;//定義sheet 10 //根據擴展名判斷解析Excel格式 11 fis = new FileInputStream(filePath); 12 workbook = new XSSFWorkbook(fis);//獲取工作簿 13 Map<String, PictureData> maplist = null; 14 //開始遍歷Excel表中多個sheet 15 for(int i = 0; i < workbook.getNumberOfSheets(); i++){//工作簿中多個sheet,循環遍歷出獲取每個sheet表中數據 16 sheet = workbook.getSheetAt(i);//獲取sheet表 17 String sheetName = sheet.getSheetName().replaceAll("\\s*", "");//獲取sheet名稱 18 maplist = getXlsxPicture((XSSFSheet)sheet); 19 //取存圖片 20 try { 21 getExcelImage(maplist, fileName, sheetName); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 //開始解析表格數據 26 Date date;//定義所有日期 27 //獲取數據的總行數 28 int totalRowNum = sheet.getLastRowNum(); 29 //定義獲取的數據屬性名稱 30 String filingNum = "";//申請/注冊號 31 String type = "";//類型 32 String mark = "";//圖樣 33 String properter = "";//申請人 34 String filingDate = "";//申請日期 35 String startDate = "";//開始日期 36 String endDate = "";//結束日期 37 String filingAddress = "";//申請地址 38 String goodsDescriptionCN = "";//商品中文描述 39 String goodsDescriptionEN = "";//商品英文描述 40 for(int m = 1; m <= totalRowNum; m++){ 41 Row row = sheet.getRow(m);//獲取第 m 行的數據 42 //獲取每列數據 43 Cell cell = row.getCell(1); 44 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請/注冊號 45 cell.setCellType(CellType.STRING); 46 filingNum = cell.getStringCellValue(); 47 exContent = exContent +"&@#filingNum&@#"+filingNum; 48 } 49 cell = row.getCell(2); 50 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************類型 51 cell.setCellType(CellType.STRING); 52 type = cell.getStringCellValue(); 53 exContent = exContent + "&@#type&@#" + type; 54 } 55 cell = row.getCell(3);//此處圖片得到的單元格內容為空值 56 String picPath = "D:/TradeMark/Tomcat8/webapps/ROOT/trademarkimage/ReferencePicture/" + fileName + "/" + sheetName;//定義圖片存儲地址 57 if(cell.getCellTypeEnum() == CellType.BLANK){//****************************************存儲圖片地址 58 cell.setCellType(CellType.STRING); 59 mark = picPath + "/" + (m+1) + "_" + 4 + ".jpg"; 60 exContent = exContent + "&@#mark&@#" + mark; 61 } 62 cell = row.getCell(4); 63 String[] filingPerson; 64 String CNproperter = ""; 65 String ENproperter = ""; 66 String regp = "[^\u4e00-\u9fa5]";//定義中文正則表達式 67 String sp = ""; 68 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請人 69 cell.setCellType(CellType.STRING); 70 properter = cell.getStringCellValue(); 71 sp = properter.replaceAll(regp, ""); 72 if(!sp.equals("") && !sp.equals(null)){ 73 if(properter.contains(";")){ 74 filingPerson = properter.split(";"); 75 CNproperter = filingPerson[0].toString(); 76 ENproperter = filingPerson[1].toUpperCase(); 77 exContent = exContent + "&@#CNproperter&@#" + CNproperter; 78 exContent = exContent + "&@#ENproperter&@#" + ENproperter; 79 }else if(properter.contains(";")){ 80 filingPerson = properter.split(";"); 81 CNproperter = filingPerson[0].toString(); 82 ENproperter = filingPerson[1].toUpperCase(); 83 exContent = exContent + "&@#CNproperter&@#" + CNproperter; 84 exContent = exContent + "&@#ENproperter&@#" + ENproperter; 85 }else{ 86 CNproperter = properter; 87 exContent = exContent + "&@#CNproperter&@#" + CNproperter; 88 exContent = exContent + "&@#ENproperter&@#" + ENproperter; 89 } 90 }else{ 91 ENproperter = properter; 92 exContent = exContent + "&@#CNproperter&@#" + CNproperter; 93 exContent = exContent + "&@#ENproperter&@#" + ENproperter; 94 } 95 } 96 cell = row.getCell(5); 97 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************申請日期 98 if (cell.getCellTypeEnum() == CellType.STRING) {//判斷單元格內容的數據類型 99 cell.setCellType(CellType.STRING); 100 filingDate = cell.getStringCellValue(); 101 exContent = exContent + "&@#filingDate&@#" + filingDate; 102 }else{ 103 cell.setCellType(CellType.NUMERIC); 104 date = cell.getDateCellValue(); 105 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 106 filingDate = sdf.format(date); 107 exContent = exContent + "&@#filingDate&@#" + filingDate; 108 } 109 }else{//申請日期單元格為空 110 cell.setCellType(CellType.STRING); 111 filingDate = cell.getStringCellValue(); 112 exContent = exContent + "&@#filingDate&@#" + filingDate; 113 } 114 cell = row.getCell(6); 115 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************注冊日期 116 if (cell.getCellTypeEnum() == CellType.STRING) { 117 cell.setCellType(CellType.STRING); 118 startDate = cell.getStringCellValue(); 119 exContent = exContent + "&@#startDate&@#" + startDate; 120 }else{ 121 cell.setCellType(CellType.NUMERIC); 122 date = cell.getDateCellValue(); 123 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 124 startDate = sdf.format(date); 125 exContent = exContent + "&@#startDate&@#" + startDate; 126 } 127 }else{//注冊日期單元格為空 128 cell.setCellType(CellType.STRING); 129 startDate = cell.getStringCellValue(); 130 exContent = exContent + "&@#startDate&@#" + startDate; 131 } 132 cell = row.getCell(7); 133 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************注冊到期日 134 if (cell.getCellTypeEnum() == CellType.STRING) { 135 cell.setCellType(CellType.STRING); 136 endDate = cell.getStringCellValue(); 137 exContent = exContent + "&@#endDate&@#" + endDate; 138 }else{ 139 cell.setCellType(CellType.NUMERIC); 140 date = cell.getDateCellValue(); 141 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 142 endDate = sdf.format(date); 143 exContent = exContent + "&@#endDate&@#" + endDate; 144 } 145 }else{//注冊到期日單元格為空 146 cell.setCellType(CellType.STRING); 147 endDate = cell.getStringCellValue(); 148 exContent = exContent + "&@#endDate&@#" + endDate; 149 } 150 cell = row.getCell(8); 151 String[] address;//定義注冊地址分割 152 String CNAddress = "";//定義中文地址 153 String ENAddress = "";//定義英文地址 154 String reg = "[^\u4e00-\u9fa5]";//定義中文正則表達式 155 String cc = ""; 156 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************注冊地址 157 cell.setCellType(CellType.STRING); 158 filingAddress = cell.getStringCellValue(); 159 cc = filingAddress.replaceAll(reg, ""); 160 if(!cc.equals(null) && !cc.equals("")){ 161 if(filingAddress.contains(";")){ 162 address = filingAddress.split(";"); 163 CNAddress = address[0].toString();//截取中文地址 164 ENAddress = address[1].toUpperCase();//截取英文地址並轉換成大寫 165 exContent = exContent + "&@#CNAddress&@#" + CNAddress; 166 exContent = exContent + "&@#ENAddress&@#" + ENAddress; 167 }else{ 168 address = filingAddress.split(";"); 169 CNAddress = address[0].toString();//截取中文地址 170 ENAddress = address[1].toUpperCase();//截取英文地址並轉換成大寫 171 exContent = exContent + "&@#CNAddress&@#" + CNAddress; 172 exContent = exContent + "&@#ENAddress&@#" + ENAddress; 173 } 174 }else{ 175 ENAddress = filingAddress; 176 exContent = exContent + "&@#CNAddress&@#" + CNAddress; 177 exContent = exContent + "&@#ENAddress&@#" + ENAddress; 178 } 179 }else{//注冊地址為空 180 exContent = exContent + "&@#CNAddress&@#" + CNAddress; 181 exContent = exContent + "&@#ENAddress&@#" + ENAddress; 182 } 183 cell = row.getCell(9); 184 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************商品中文描述 185 cell.setCellType(CellType.STRING); 186 goodsDescriptionCN = cell.getStringCellValue(); 187 exContent = exContent + "&@#goodsDescriptionCN&@#" + goodsDescriptionCN; 188 }else{//商品中文描述為空 189 exContent = exContent + "&@#goodsDescriptionCN&@#" + goodsDescriptionCN; 190 } 191 cell = row.getCell(10); 192 if(cell.getCellTypeEnum() != CellType.BLANK){//****************************************商品英文描述 193 cell.setCellType(CellType.STRING); 194 goodsDescriptionEN = cell.getStringCellValue(); 195 exContent = exContent + "&@#goodsDescriptionEN&@#" + goodsDescriptionEN; 196 }else{//商品英文描述為空 197 exContent = exContent + "&@#goodsDescriptionEN&@#" + goodsDescriptionEN; 198 } 199 } 200 } 201 return exContent; 202 }
上述解析文檔方法工具方法:
1 /** 2 * 一鍵解析所有文檔工具方法,獲取多層文件夾下全部的最終文件 3 * @param filePath 4 * @param listName 5 */ 6 public static void getAllFiles(String filePath, ArrayList<String> listName){ 7 File file = new File(filePath); 8 File[] files = file.listFiles(); 9 String[] names = file.list(); 10 if (names != null) { 11 String[] completNames = new String[names.length]; 12 for(int i = 0; i < names.length; i++){ 13 completNames[i] = filePath + names[i]; 14 } 15 listName.addAll(Arrays.asList(completNames)); 16 } 17 for(File f : files){ 18 if (f.isDirectory()) { 19 getAllFiles(f.getAbsolutePath()+"\\", listName); 20 } 21 } 22 } 23 24 25 26 /** 27 * 拷貝文件,文件夾及子文件夾 28 * @param data 29 * @param dest 30 */ 31 public static void copyFolderAndFiles(String oldPath, String newPath){ 32 try { 33 File file = new File(newPath); 34 if(!file.exists()){ 35 file.mkdirs(); 36 } 37 File oFile = new File(oldPath); 38 String[] files = oFile.list(); 39 File temp = null; 40 for(int i = 0; i < files.length; i++){ 41 if(oldPath.endsWith(File.separator)){ 42 temp = new File(oldPath+files[i]); 43 }else{ 44 temp = new File(oldPath+File.separator+files[i]); 45 } 46 if(temp.isFile()){//判斷是否為文件 47 FileInputStream fis = new FileInputStream(temp); 48 FileOutputStream fos = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); 49 byte[] bs = new byte[1024*5]; 50 int len; 51 while((len=fis.read(bs)) != -1){ 52 fos.write(bs, 0, len); 53 } 54 fos.flush(); 55 fos.close(); 56 fis.close(); 57 } 58 if(temp.isDirectory()){ 59 copyFolderAndFiles(oldPath+"/"+files[i], newPath+"/"+files[i]); 60 } 61 } 62 } catch (FileNotFoundException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } catch (IOException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } 69 }
Excel表格工具方法(圖片)
1 /** 2 * 解析獲取2003版xls文件的圖片 3 * @param sheet 4 * @return 5 */ 6 public static Map<String, PictureData> getXlsPicture(HSSFSheet sheet){ 7 Map<String, PictureData> map = new HashMap<String, PictureData>(); 8 List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren(); 9 for(HSSFShape shape : list){ 10 if(shape instanceof HSSFPicture){ 11 HSSFPicture picture = (HSSFPicture) shape; 12 HSSFClientAnchor anchor = (HSSFClientAnchor) picture.getAnchor(); 13 PictureData data = picture.getPictureData(); 14 String key = (anchor.getRow1()+1) + "_" + (anchor.getCol1()+1); 15 map.put(key, data); 16 } 17 } 18 return map; 19 } 20 21 22 public static Map<String, PictureData> getXlsPicture2(HSSFSheet sheet, HSSFWorkbook workbook){ 23 Map<String, PictureData> map = new HashMap<String, PictureData>(); 24 List<HSSFPictureData> list = workbook.getAllPictures(); 25 if(!list.isEmpty()){ 26 for(HSSFShape shape : sheet.getDrawingPatriarch().getChildren()){ 27 HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); 28 if(shape instanceof HSSFPicture){ 29 HSSFPicture picture = (HSSFPicture) shape; 30 int pictureIndex = picture.getPictureIndex() - 1; 31 HSSFPictureData pictureData = list.get(pictureIndex); 32 String picIndex = String.valueOf(anchor.getRow1()+1) + "_" + String.valueOf(anchor.getCol1()+1); 33 map.put(picIndex, pictureData); 34 } 35 } 36 return map; 37 }else{ 38 return map; 39 } 40 } 41 42 43 44 /** 45 * 解析獲取2007版xlsx文件的圖片 46 * @param sheet 47 * @return 48 */ 49 public static Map<String, PictureData> getXlsxPicture(XSSFSheet sheet){ 50 Map<String, PictureData> map = new HashMap<String, PictureData>(); 51 List<POIXMLDocumentPart> list = sheet.getRelations(); 52 for(POIXMLDocumentPart part : list){ 53 if(part instanceof XSSFDrawing){ 54 XSSFDrawing drawing = (XSSFDrawing) part; 55 List<XSSFShape> shapes = drawing.getShapes(); 56 for(XSSFShape shape : shapes){ 57 XSSFPicture picture = (XSSFPicture) shape; 58 XSSFClientAnchor anchor = picture.getPreferredSize(); 59 CTMarker marker = anchor.getFrom(); 60 String key = (marker.getRow()+1) + "_" + (marker.getCol()+1); 61 map.put(key, picture.getPictureData()); 62 } 63 } 64 } 65 return map; 66 } 67 68 /** 69 * 解析輸出保存獲取的圖片 70 * @throws Exception 71 */ 72 public static void getExcelImage(Map<String, PictureData> sheetlist, String fileName, String sheetName) throws Exception{ 73 Object[] key = sheetlist.keySet().toArray(); 74 String filepath = ""; 75 for(int i = 0; i < sheetlist.size(); i++){ 76 PictureData data = sheetlist.get(key[i]); 77 String picName = key[i].toString(); 78 byte[] bs = data.getData(); 79 String path = "D:/TradeMark/Tomcat8/webapps/ROOT/trademarkimage/ReferencePicture/" + fileName + "/" + sheetName; 80 filepath = path + "/" + picName + ".jpg"; 81 File file = new File(path); 82 if(!file.exists()){ 83 file.mkdirs(); 84 } 85 //保存輸出圖片 86 FileOutputStream fos = new FileOutputStream(filepath); 87 fos.write(bs); 88 fos.close(); 89 } 90 }