java用poi解析excel2003和2007并封装成对象返回

网上看了很多资料 比价乱 而且 质量参差不齐 ,自己 通过实践和看资料学习 

完整的 总结了这两个java里常用的工具类 
整合成两个通用的方法 

为了取值更加方便我用了 json来 组装数据 
其实 最新的poi3.9已经可以用一个方法来 读取excel2003和2007了 
但是我为了业务逻辑更加清楚才分开来 ,如果 你觉得还可以重构的更好 可以 发出你的改良后的代码哦 


Java代码   收藏代码
  1. import java.io.BufferedOutputStream;  
  2. import java.io.ByteArrayInputStream;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.util.Date;  
  11. import java.util.LinkedHashMap;  
  12. import java.util.LinkedList;  
  13. import java.util.List;  
  14. import java.util.Map;  
  15.   
  16. import net.sf.json.JSONObject;  
  17.   
  18. import org.apache.poi.hssf.usermodel.HSSFCell;  
  19. import org.apache.poi.hssf.usermodel.HSSFRow;  
  20. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  21. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  22. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  23. import org.apache.poi.xssf.usermodel.XSSFCell;  
  24. import org.apache.poi.xssf.usermodel.XSSFRow;  
  25. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  27.   
  28.   
  29. /** 
  30.  * Excel  操作工具类 其中涉及几个文件流操作和转换 
  31.  * @author zqb 
  32.  * 
  33.  * 2013-8-2 
  34.  */  
  35. public class ExcelUtil {  
  36.     private static POIFSFileSystem fs;  
  37.     private static HSSFWorkbook wb;  
  38.     private static HSSFSheet sheet;  
  39.     private static HSSFRow row;  
  40.     private static FileInputStream input;  
  41.     private static String[] excleTitle;  
  42.   
  43.     public static boolean isNum(String str) {  
  44.         return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");  
  45.     }  
  46.   
  47.     /** 
  48.      * 根据文件路径读取Excel数据内容 返回map 
  49.      * @param excelPath 
  50.      * @return 
  51.      */  
  52.     public static Map<Integer, JSONObject> readExcelContent(String excelPath) {  
  53.         Map<Integer, JSONObject> contentJson = new LinkedHashMap<Integer, JSONObject>();  
  54.         String excelStr = "";// excel 内容  
  55.         try {  
  56.             input = new FileInputStream(new File(excelPath));  
  57.   
  58.             fs = new POIFSFileSystem(input);  
  59.             wb = new HSSFWorkbook(fs);  
  60.             sheet = wb.getSheetAt(0);  
  61.             int rowNum = sheet.getLastRowNum(); // 得到总行数  
  62.             row = sheet.getRow(0);// 得到标题的内容对象。  
  63.             int colNum = row.getPhysicalNumberOfCells();// 得到每行的列数。  
  64.   
  65.             excleTitle = new String[colNum];  
  66.             for (int i = 0; i < colNum; i++) {  
  67.   
  68.                 excleTitle[i] = getStringCellValue(row.getCell((short) i));  
  69.             }  
  70.   
  71.             // 正文内容应该从第二行开始,第一行为表头的标题  
  72.             for (int i = 1; i <= rowNum; i++) {  
  73.   
  74.                 row = sheet.getRow(i);  
  75.                 int j = 0;  
  76.   
  77.                 while (j < colNum) {  
  78.                     String v = "";  
  79.                     if (j + 1 == colNum) {  
  80.                         String vs = getStringCellValue(row.getCell((short) j))  
  81.                                 .trim();  
  82.                         if (vs.indexOf(".") > -1) {  
  83.                             if (isNum(vs)) { // 是否是数字  
  84.   
  85.                                 if (vs.endsWith("0")) {  
  86.   
  87.                                     v = vs.substring(0, vs.indexOf("."));  
  88.                                 }  
  89.   
  90.                             } else {  
  91.                                 v = vs.trim();  
  92.                             }  
  93.   
  94.                         } else {  
  95.                             v = vs.trim();  
  96.                         }  
  97.                         excelStr += v;  
  98.                     } else {  
  99.   
  100.                         String vs = getStringCellValue(row.getCell((short) j))  
  101.                                 .trim()  
  102.                                 + "&";  
  103.                         if (vs.indexOf(".") > -1) {  
  104.                             if (isNum(vs)) { // 是否是数字  
  105.                                 if (vs.endsWith("0")) { // 处理用poi读取excel整数后面加.0的格式化  
  106.                                     v = vs.substring(0, vs.indexOf("."));  
  107.                                 }  
  108.   
  109.                             } else {  
  110.                                 v = vs.trim();  
  111.                             }  
  112.   
  113.                         } else {  
  114.                             v = vs.trim();  
  115.                         }  
  116.                         excelStr += v;  
  117.                     }  
  118.   
  119.                     j++;  
  120.   
  121.                 }  
  122.                 String excelstrArray[] = excelStr.split("&", -1); // 每行数据  
  123.   
  124.                 Map<String, String> params = new LinkedHashMap<String, String>();  
  125.                 for (int k = 0; k < excelstrArray.length; k++) {  
  126.                     params.put(excleTitle[k], excelstrArray[k]);  
  127.                 }  
  128.                 JSONObject jsonObject = JSONObject.fromObject(params);  
  129.                 contentJson.put(i, jsonObject);  
  130.   
  131. //              content.put(i, excelStr);  
  132.   
  133.                 excelStr = "";  
  134.             }  
  135.   
  136.         } catch (FileNotFoundException e) {  
  137.             e.printStackTrace();  
  138.         } catch (IOException e) {  
  139.             e.printStackTrace();  
  140.         } finally {  
  141.             try {  
  142.                 if (input != null) {  
  143.                     input.close();  
  144.                 }  
  145.             } catch (IOException e) {  
  146.                 e.printStackTrace();  
  147.             }  
  148.         }  
  149.         return contentJson;  
  150.     }  
  151.       
  152.       
  153.     /** 
  154.      * 根据文件流读取Excel数据内容 返回map 2003 
  155.      * @param input 
  156.      * @return 
  157.      */  
  158.     public static Map<Integer, JSONObject> readExcelContent(  
  159.             InputStream input) {// 读取Excel数据内容  
  160.         Map<Integer, JSONObject> contentJson = new LinkedHashMap<Integer, JSONObject>();  
  161.         String excelStr = "";// excel 内容  
  162.         try {  
  163.   
  164.               
  165.             fs = new POIFSFileSystem(input);  
  166.             wb = new HSSFWorkbook(fs);  
  167.             sheet = wb.getSheetAt(0);  
  168.             int rowNum = sheet.getLastRowNum(); // 得到总行数  
  169.             row = sheet.getRow(0);// 得到标题的内容对象。  
  170.             int colNum = row.getPhysicalNumberOfCells();// 得到每行的列数。  
  171.   
  172.             excleTitle = new String[colNum];  
  173.             for (int i = 0; i < colNum; i++) {  
  174.   
  175.                 excleTitle[i] = getStringCellValue(row.getCell((short) i));  
  176.             }  
  177.   
  178.             // 正文内容应该从第二行开始,第一行为表头的标题  
  179.             for (int i = 1; i <= rowNum; i++) {  
  180.   
  181.                 row = sheet.getRow(i);  
  182.                 int j = 0;  
  183.   
  184.                 while (j < colNum) {  
  185.                     String v = "";  
  186.                     if (j + 1 == colNum) {  
  187.                         String vs = getStringCellValue(row.getCell((short) j))  
  188.                                 .trim();  
  189.                         if (vs.indexOf(".") > -1) {  
  190.                             if (isNum(vs)) { // 是否是数字  
  191.   
  192.                                 if (vs.endsWith("0")) {  
  193.   
  194.                                     v = vs.substring(0, vs.indexOf("."));  
  195.                                 }  
  196.   
  197.                             } else {  
  198.                                 v = vs.trim();  
  199.                             }  
  200.   
  201.                         } else {  
  202.                             v = vs.trim();  
  203.                         }  
  204.                         excelStr += v;  
  205.                     } else {  
  206.   
  207.                         String vs = getStringCellValue(row.getCell((short) j))  
  208.                                 .trim()  
  209.                                 + "&";  
  210.                         if (vs.indexOf(".") > -1) {  
  211.                             if (isNum(vs)) { // 是否是数字  
  212.                                 if (vs.endsWith("0")) { // 处理用poi读取excel整数后面加.0的格式化  
  213.                                     v = vs.substring(0, vs.indexOf("."));  
  214.                                 }  
  215.   
  216.                             } else {  
  217.                                 v = vs.trim();  
  218.                             }  
  219.   
  220.                         } else {  
  221.                             v = vs.trim();  
  222.                         }  
  223.                         excelStr += v;  
  224.                     }  
  225.   
  226.                     j++;  
  227.   
  228.                 }  
  229.                 String excelstrArray[] = excelStr.split("&", -1); // 每行数据  
  230.   
  231.                 Map<String, String> params = new LinkedHashMap<String, String>();  
  232.                 for (int k = 0; k < excelstrArray.length; k++) {  
  233.                     params.put(excleTitle[k], excelstrArray[k]);  
  234.                 }  
  235.                 JSONObject jsonObject = JSONObject.fromObject(params);  
  236.                 contentJson.put(i, jsonObject);  
  237.   
  238.                 // content.put(i, excelStr);  
  239.   
  240.                 excelStr = "";  
  241.             }  
  242.   
  243.         } catch (FileNotFoundException e) {  
  244.             e.printStackTrace();  
  245.         } catch (IOException e) {  
  246.             e.printStackTrace();  
  247.         } finally {  
  248.             try {  
  249.                 if (input != null) {  
  250.                     input.close();  
  251.                 }  
  252.             } catch (IOException e) {  
  253.                 e.printStackTrace();  
  254.             }  
  255.         }  
  256.         return contentJson;  
  257.     }  
  258.       
  259.     /** 
  260.      * 读取Office 2007 excel 
  261.      * */  
  262.     public static Map<Integer, JSONObject> read2007Excels(InputStream input)  
  263.             throws IOException {  
  264.         Map<Integer, JSONObject> contentJson = new LinkedHashMap<Integer, JSONObject>();  
  265.         // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  266.         XSSFWorkbook xwb = new XSSFWorkbook(input);  
  267.         // 读取第一章表格内容  
  268.         XSSFSheet sheet = xwb.getSheetAt(0);  
  269.         Object value = null;  
  270.         XSSFRow row = null;  
  271.         XSSFCell cell = null;  
  272.   
  273.         XSSFRow headerrow = sheet.getRow(0); // 表头 得到标题的内容对象  
  274.         int colNum = headerrow.getPhysicalNumberOfCells();// 得到每行的列数。  
  275.         excleTitle = new String[colNum];  
  276.         for (int i = 0; i < colNum; i++) {  
  277.   
  278.             excleTitle[i] = getStringCellValue(headerrow.getCell((short) i));  
  279.         }  
  280.   
  281.         // System.out.println(sheet.getPhysicalNumberOfRows());  
  282.         // 循环内容项 不循环标题 所以+1  
  283.         for (int i = sheet.getFirstRowNum() + 1; i <= sheet  
  284.                 .getPhysicalNumberOfRows(); i++) {  
  285.             row = sheet.getRow(i);  
  286.             if (row == null) {  
  287.                 continue;  
  288.             }  
  289.             List<String> linked = new LinkedList<String>();  
  290.             for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {  
  291.                 cell = row.getCell(j);  
  292.                 if (null != cell) {  
  293.                     value = getStringCellValue(cell);  
  294.                 }  
  295.   
  296.                 linked.add(StringUtil.stringIsNull(value));  
  297.             }  
  298.             Map<String, String> params = new LinkedHashMap<String, String>();  
  299.             for (int j = 0; j < linked.size(); j++) {  
  300.                 params.put(excleTitle[j], linked.get(j));  
  301.             }  
  302.             JSONObject jsonObject = JSONObject.fromObject(params);  
  303.             contentJson.put(i, jsonObject);  
  304.         }  
  305.   
  306.         return contentJson;  
  307.     }  
  308.       
  309.     /** 
  310.      * 根据(字节串(或叫字节数组)变成输入流的形式)读取Excel数据内容 返回map 
  311.      * @param input 
  312.      * @return 
  313.      */  
  314.     public static Map<Integer, JSONObject> readExcelContent(  
  315.             ByteArrayInputStream input) {// 读取Excel数据内容  
  316.         // Map<Integer, String> content = new HashMap<Integer, String>();  
  317.         Map<Integer, JSONObject> contentJson = new LinkedHashMap<Integer, JSONObject>();  
  318.         String excelStr = "";// excel 内容  
  319.         try {  
  320.   
  321. //          ByteArrayInputStream is = new ByteArrayInputStream( new byte[1000]);  
  322.               
  323.             fs = new POIFSFileSystem(input);  
  324.             wb = new HSSFWorkbook(fs);  
  325.             sheet = wb.getSheetAt(0);  
  326.             int rowNum = sheet.getLastRowNum(); // 得到总行数  
  327.             row = sheet.getRow(0);// 得到标题的内容对象。  
  328.             int colNum = row.getPhysicalNumberOfCells();// 得到每行的列数。  
  329.   
  330.             excleTitle = new String[colNum];  
  331.             for (int i = 0; i < colNum; i++) {  
  332.   
  333.                 excleTitle[i] = getStringCellValue(row.getCell((short) i));  
  334.             }  
  335.   
  336.             // 正文内容应该从第二行开始,第一行为表头的标题  
  337.             for (int i = 1; i <= rowNum; i++) {  
  338.   
  339.                 row = sheet.getRow(i);  
  340.                 int j = 0;  
  341.   
  342.                 while (j < colNum) {  
  343.                     String v = "";  
  344.                     if (j + 1 == colNum) {  
  345.                         String vs = getStringCellValue(row.getCell((short) j))  
  346.                                 .trim();  
  347.                         if (vs.indexOf(".") > -1) {  
  348.                             if (isNum(vs)) { // 是否是数字  
  349.   
  350.                                 if (vs.endsWith("0")) {  
  351.   
  352.                                     v = vs.substring(0, vs.indexOf("."));  
  353.                                 }  
  354.   
  355.                             } else {  
  356.                                 v = vs.trim();  
  357.                             }  
  358.   
  359.                         } else {  
  360.                             v = vs.trim();  
  361.                         }  
  362.                         excelStr += v;  
  363.                     } else {  
  364.   
  365.                         String vs = getStringCellValue(row.getCell((short) j))  
  366.                                 .trim()  
  367.                                 + "&";  
  368.                         if (vs.indexOf(".") > -1) {  
  369.                             if (isNum(vs)) { // 是否是数字  
  370.                                 if (vs.endsWith("0")) { // 处理用poi读取excel整数后面加.0的格式化  
  371.                                     v = vs.substring(0, vs.indexOf("."));  
  372.                                 }  
  373.   
  374.                             } else {  
  375.                                 v = vs.trim();  
  376.                             }  
  377.   
  378.                         } else {  
  379.                             v = vs.trim();  
  380.                         }  
  381.                         excelStr += v;  
  382.                     }  
  383.   
  384.                     j++;  
  385.   
  386.                 }  
  387.                 String excelstrArray[] = excelStr.split("&", -1); // 每行数据  
  388.   
  389.                 Map<String, String> params = new LinkedHashMap<String, String>();  
  390.                 for (int k = 0; k < excelstrArray.length; k++) {  
  391.                     params.put(excleTitle[k], excelstrArray[k]);  
  392.                 }  
  393.                 JSONObject jsonObject = JSONObject.fromObject(params);  
  394.                 contentJson.put(i, jsonObject);  
  395.   
  396.                 // content.put(i, excelStr);  
  397.   
  398.                 excelStr = "";  
  399.             }  
  400.   
  401.         } catch (FileNotFoundException e) {  
  402.             e.printStackTrace();  
  403.         } catch (IOException e) {  
  404.             e.printStackTrace();  
  405.         } finally {  
  406.             try {  
  407.                 if (input != null) {  
  408.                     input.close();  
  409.                 }  
  410.             } catch (IOException e) {  
  411.                 e.printStackTrace();  
  412.             }  
  413.         }  
  414.         return contentJson;  
  415.     }  
  416.   
  417.     /** 
  418.      * 获取单元格数据内容为字符串类型的数据 
  419.      * @param cell 
  420.      * @return 
  421.      */  
  422.     private static String getStringCellValue(HSSFCell cell) {  
  423.         String strCell = "";  
  424.         switch (cell.getCellType()) {  
  425.         case HSSFCell.CELL_TYPE_STRING:  
  426.             strCell = cell.getStringCellValue();  
  427.             break;  
  428.         case HSSFCell.CELL_TYPE_NUMERIC:  
  429.             strCell = String.valueOf(cell.getNumericCellValue());  
  430.             break;  
  431.         case HSSFCell.CELL_TYPE_BOOLEAN:  
  432.             strCell = String.valueOf(cell.getBooleanCellValue());  
  433.             break;  
  434.         case HSSFCell.CELL_TYPE_BLANK:  
  435.             strCell = "";  
  436.             break;  
  437.         default:  
  438.             strCell = "";  
  439.             break;  
  440.         }  
  441.         if (strCell.equals("") || strCell == null) {  
  442.             return "";  
  443.         }  
  444.         if (cell == null) {  
  445.             return "";  
  446.         }  
  447.         return strCell;  
  448.     }  
  449.   
  450.     /** 
  451.      * 获取单元格数据内容为日期类型的数据 
  452.      * @param cell 
  453.      * @return 
  454.      */  
  455.     private static String getDateCellValue(HSSFCell cell) {  
  456.         String result = "";  
  457.         try {  
  458.             int cellType = cell.getCellType();  
  459.             if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {  
  460.                 Date date = cell.getDateCellValue();  
  461.                 result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)  
  462.                         + "-" + date.getDate();  
  463.             } else if (cellType == HSSFCell.CELL_TYPE_STRING) {  
  464.                 String date = getStringCellValue(cell);  
  465.                 result = date.replaceAll("[年月]""-").replace("日""").trim();  
  466.             } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {  
  467.                 result = "";  
  468.             }  
  469.         } catch (Exception e) {  
  470.             System.out.println("日期格式不正确!");  
  471.             e.printStackTrace();  
  472.         }  
  473.         return result;  
  474.     }  
  475.   
  476.     /** 
  477.      * 根据byte数组,生成文件 
  478.      */  
  479.     public static void getFile(byte[] bfile, String filePath, String fileName) {  
  480.         BufferedOutputStream bos = null;  
  481.         FileOutputStream fos = null;  
  482.         File file = null;  
  483.         try {  
  484.             File dir = new File(filePath);  
  485.             if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在  
  486.                 dir.mkdirs();  
  487.             }  
  488.             file = new File(filePath + "\\" + fileName);  
  489.             fos = new FileOutputStream(file);  
  490.             bos = new BufferedOutputStream(fos);  
  491.             bos.write(bfile);  
  492.         } catch (Exception e) {  
  493.             e.printStackTrace();  
  494.         } finally {  
  495.             if (bos != null) {  
  496.                 try {  
  497.                     bos.close();  
  498.                 } catch (IOException e1) {  
  499.                     e1.printStackTrace();  
  500.                 }  
  501.             }  
  502.             if (fos != null) {  
  503.                 try {  
  504.                     fos.close();  
  505.                 } catch (IOException e1) {  
  506.                     e1.printStackTrace();  
  507.                 }  
  508.             }  
  509.         }  
  510.     }  
  511.   
  512.     // 从byte[]转file  
  513.     public static File getFileFromBytes(byte[] b, String outputFile) {  
  514.         BufferedOutputStream stream = null;  
  515.         File file = null;  
  516.         try {  
  517.             file = new File(outputFile);  
  518.             if (!file.exists() && file.isDirectory()) {// 判断文件目录是否存在  
  519.                 file.mkdirs();  //mkdirs() 可以在不存在的目录中创建文件夹。诸如:a\\b,既可以创建多级目录。  
  520.             }  
  521.   
  522.             FileOutputStream fstream = new FileOutputStream(file);  
  523.             stream = new BufferedOutputStream(fstream);  
  524.             stream.write(b);  
  525.         } catch (Exception e) {  
  526.             e.printStackTrace();  
  527.         } finally {  
  528.             if (stream != null) {  
  529.                 try {  
  530.                     stream.close();  
  531.                 } catch (IOException e1) {  
  532.                     e1.printStackTrace();  
  533.                 }  
  534.             }  
  535.         }  
  536.         return file;  
  537.     }  
  538.   
  539.     /** 
  540.      * 读取Office 2007 excel 
  541.      * */  
  542.     private static Map<Integer, JSONObject> read2007Excels(File file)  
  543.             throws IOException {  
  544.         Map<Integer, JSONObject> contentJson = new LinkedHashMap<Integer, JSONObject>();  
  545.         // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  546.         XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  547.         // 读取第一章表格内容  
  548.         XSSFSheet sheet = xwb.getSheetAt(0);  
  549.         Object value = null;  
  550.         XSSFRow row = null;  
  551.         XSSFCell cell = null;  
  552.   
  553.         XSSFRow headerrow = sheet.getRow(0); // 表头 得到标题的内容对象  
  554.         int colNum = headerrow.getPhysicalNumberOfCells();// 得到每行的列数。  
  555.         excleTitle = new String[colNum];  
  556.         for (int i = 0; i < colNum; i++) {  
  557.   
  558.             excleTitle[i] = getStringCellValue(headerrow.getCell((short) i));  
  559.         }  
  560.   
  561.         // System.out.println(sheet.getPhysicalNumberOfRows());  
  562.         // 循环内容项 不循环标题 所以+1  
  563.         for (int i = sheet.getFirstRowNum() + 1; i <= sheet  
  564.                 .getPhysicalNumberOfRows(); i++) {  
  565.             row = sheet.getRow(i);  
  566.             if (row == null) {  
  567.                 continue;  
  568.             }  
  569.             List<String> linked = new LinkedList<String>();  
  570.             for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {  
  571.                 cell = row.getCell(j);  
  572.                 if (null != cell) {  
  573.                     value = getStringCellValue(cell);  
  574.                 }  
  575.   
  576.                 linked.add(StringUtil.stringIsNull(value));  
  577.             }  
  578.             Map<String, String> params = new LinkedHashMap<String, String>();  
  579.             for (int j = 0; j < linked.size(); j++) {  
  580.                 params.put(excleTitle[j], linked.get(j));  
  581.             }  
  582.             JSONObject jsonObject = JSONObject.fromObject(params);  
  583.             contentJson.put(i, jsonObject);  
  584.         }  
  585.   
  586.         return contentJson;  
  587.     }  
  588.   
  589.     /** 
  590.      * 获取单元格数据内容为字符串类型的数据 
  591.      *  
  592.      * @param cell 
  593.      * @return 
  594.      */  
  595.     private static String getStringCellValue(XSSFCell cell) {  
  596.         String strCell = "";  
  597.         switch (cell.getCellType()) {  
  598.         case HSSFCell.CELL_TYPE_STRING:  
  599.             strCell = cell.getStringCellValue();  
  600.             break;  
  601.         case HSSFCell.CELL_TYPE_NUMERIC:  
  602.             strCell = String.valueOf(cell.getNumericCellValue());  
  603.             break;  
  604.         case HSSFCell.CELL_TYPE_BOOLEAN:  
  605.             strCell = String.valueOf(cell.getBooleanCellValue());  
  606.             break;  
  607.         case HSSFCell.CELL_TYPE_BLANK:  
  608.             strCell = "";  
  609.             break;  
  610.         default:  
  611.             strCell = "";  
  612.             break;  
  613.         }  
  614.         if (strCell.equals("") || strCell == null) {  
  615.             return "";  
  616.         }  
  617.         if (cell == null) {  
  618.             return "";  
  619.         }  
  620.         return strCell;  
  621.     }  
  622.       
  623.     /** 
  624.      * 获得指定文件的byte数组 
  625.      */  
  626.     public static byte[] getBytes(String filePath) {  
  627.         byte[] buffer = null;  
  628.         try {  
  629.             File file = new File(filePath);  
  630.             FileInputStream fis = new FileInputStream(file);  
  631.             ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
  632.             byte[] b = new byte[1000];  
  633.             int n;  
  634.             while ((n = fis.read(b)) != -1) {  
  635.                 bos.write(b, 0, n);  
  636.             }  
  637.             fis.close();  
  638.             bos.close();  
  639.             buffer = bos.toByteArray();  
  640.         } catch (FileNotFoundException e) {  
  641.             e.printStackTrace();  
  642.         } catch (IOException e) {  
  643.             e.printStackTrace();  
  644.         }  
  645.         return buffer;  
  646.     }  



调用的时候 根据文件后缀名分别调用不同的方法就行 

测试 
Java代码   收藏代码
  1.     Map<Integer, JSONObject> content = new LinkedHashMap<Integer, JSONObject>();  
  2.         if (version.equals("2003")) {  
  3.             content = ExcelUtil.readExcelContent(fileinput);  
  4.         } else if (version.equals("2007")) {  
  5.             content = ExcelUtil.read2007Excels(fileinput);  
  6.         }  
  7.   
  8.         for (Map.Entry<Integer, JSONObject> entry : content.entrySet()) {  
  9. System.out.println(entry.getValue().get("excel表头的名称")));  
  10. //这个循环里可以把这些值 组装到一个 po里  
  11. //这样取值更加方便  
  12. //比如  Po p=new Po();  po.setEntityName(entry.getValue().get("excel表头的名称")));  
  13. //....具体怎么扩展 更加适合你的项目 当然要你们自己思考了 呵呵...  
  14. }  
  15.   
  16.           


这样可以获取对应表头的值
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值