[Java] java 用PIO读取Excel →→→→→进入此内容的聊天室

来自 , 2020-04-05, 写在 Java, 查看 108 次.
URL http://www.code666.cn/view/65cc2c82
  1. package com.neusoft.counter.util;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import org.apache.commons.logging.Log;
  14. import org.apache.commons.logging.LogFactory;
  15. import org.apache.poi.hssf.usermodel.HSSFCell;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  20.  
  21. import com.neusoft.counter.vo.LoginIdStaffNo;
  22.  
  23. public class ExcelDemo {
  24.  
  25.     private static final Log log = LogFactory.getLog(ExcelDemo.class);
  26.  
  27.     public List parseExcel(File in) {
  28.         List arrayList = new ArrayList();
  29.  
  30.         FileInputStream fis = null;
  31.         POIFSFileSystem fs = null;
  32.  
  33.         try {
  34.             fis = new FileInputStream(in);
  35.             fs = new POIFSFileSystem(fis);
  36.  
  37.             HSSFWorkbook wb = new HSSFWorkbook(fs);
  38.             // first sheet
  39.             HSSFSheet sheet = wb.getSheetAt(0);
  40.             int lastRow = sheet.getLastRowNum();
  41.  
  42.             HSSFRow row = null;
  43.             HSSFCell cell = null;
  44.             int columnNum = row.getLastCellNum();
  45.             String data[] = new String[2];
  46.  
  47.             // 读取Excel表格
  48.             for (int i = 1; i <= lastRow; i++) { // 行循环
  49.                 row = sheet.getRow(i);
  50.  
  51.                 for (int j = 0; j < columnNum; j++) { // 列循环
  52.                     cell = row.getCell((short) j);
  53.                     if (cell != null && cell.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
  54.                         data[j] = cell.getStringCellValue().trim();
  55.  
  56.                     }
  57.                 }
  58.  
  59.                 //TODO add to List
  60.             }
  61.  
  62.         }
  63.         catch (FileNotFoundException e) {
  64.             log.error(e);
  65.         }
  66.         catch (IOException e) {
  67.             log.error(e);
  68.         }
  69.  
  70.         return arrayList;
  71.  
  72.     }
  73.  
  74.     public void writeToExcel(Map map, File outFile) throws IOException {
  75.         if (map == null) {
  76.             log.info("没有输出到excel的数据!");
  77.             return;
  78.         }
  79.         HSSFWorkbook wb = new HSSFWorkbook();
  80.         HSSFSheet sheet = wb.createSheet();
  81.  
  82.         // 标题
  83.         HSSFRow title = sheet.createRow(0);
  84.         HSSFCell userCell = title.createCell((short) 0), staffCell = title.createCell((short) 1), _infoCell = title
  85.                 .createCell((short) 2);
  86.         userCell.setEncoding(HSSFCell.ENCODING_UTF_16);
  87.         userCell.setCellValue("后台用户");
  88.         staffCell.setEncoding(HSSFCell.ENCODING_UTF_16);
  89.         staffCell.setCellValue("柜员号");
  90.         _infoCell.setEncoding(HSSFCell.ENCODING_UTF_16);
  91.         _infoCell.setCellValue("失败原因");
  92.  
  93.         for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {
  94.             String key = (String) itr.next();
  95.             List list = (List) map.get(key);
  96.             String info = "";
  97.             if ("1".equals(key))
  98.                 info = "后台用户不存在";
  99.             else if ("2".equals(key))
  100.                 info = "柜员号重复";
  101.             else if ("3".equals(key))
  102.                 info = "插入数据库出错";
  103.  
  104.             appendToSheet(sheet, list, info);
  105.         }
  106.  
  107.         FileOutputStream fos = new FileOutputStream(outFile);
  108.  
  109.         wb.write(fos);
  110.         fos.close();
  111.  
  112.     }
  113.  
  114.     private void appendToSheet(HSSFSheet sheet, List datas, String info) {
  115.  
  116.         if (datas == null)
  117.             return;
  118.         int offset = sheet.getLastRowNum();
  119.         int size = datas.size();
  120.         for (int i = 0; i < size; i++) {
  121.             LoginIdStaffNo ls = (LoginIdStaffNo) datas.get(i);
  122.             HSSFRow row = sheet.createRow(offset + i + 1);
  123.             row.createCell((short) 0).setCellValue(ls.getUserLoginId());
  124.             row.createCell((short) 1).setCellValue(ls.getStaffNo());
  125.  
  126.             HSSFCell infoCell = row.createCell((short) 2);
  127.             infoCell.setEncoding(HSSFCell.ENCODING_UTF_16);
  128.             infoCell.setCellValue(info);
  129.         }
  130.     }
  131. }
  132.  
  133. //源代码片段来自云代码http://yuncode.net
  134.                        

回复 "java 用PIO读取Excel"

这儿你可以回复上面这条便签

captcha