java 解析Word文档以及Excel表格数据带有图片(2003/2007)

一、解析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     }

猜你喜欢

转载自www.cnblogs.com/xiaoyue1606bj/p/11576876.html