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

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

回复 "java PIO读取Excel文件"

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

captcha