java poi技术操作excel之向Excel中写入值

原文地址https://blog.csdn.net/u013068377/article/details/52164499

[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.OutputStream;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10. import org.apache.poi.ss.usermodel.Cell;  
  11. import org.apache.poi.ss.usermodel.Row;  
  12. import org.apache.poi.ss.usermodel.Sheet;  
  13. import org.apache.poi.ss.usermodel.Workbook;  
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  15.   
  16. public class WriteExcel {  
  17.     private static final String EXCEL_XLS = "xls";  
  18.     private static final String EXCEL_XLSX = "xlsx";  
  19.   
  20.     public static void writeExcel(List<Map> dataList, int cloumnCount,String finalXlsxPath){  
  21.         OutputStream out = null;  
  22.         try {  
  23.             // 获取总列数  
  24.             int columnNumCount = cloumnCount;  
  25.             // 读取Excel文档  
  26.             File finalXlsxFile = new File(finalXlsxPath);  
  27.             Workbook workBook = getWorkbok(finalXlsxFile);  
  28.             // sheet 对应一个工作页  
  29.             Sheet sheet = workBook.getSheetAt(0);  
  30.             /** 
  31.              * 删除原有数据,除了属性列 
  32.              */  
  33.             int rowNumber = sheet.getLastRowNum();  // 第一行从0开始算  
  34.             System.out.println("原始数据总行数,除属性列:" + rowNumber);  
  35.             for (int i = 1; i <= rowNumber; i++) {  
  36.                 Row row = sheet.getRow(i);  
  37.                 sheet.removeRow(row);  
  38.             }  
  39.             // 创建文件输出流,输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效  
  40.             out =  new FileOutputStream(finalXlsxPath);  
  41.             workBook.write(out);  
  42.             /** 
  43.              * 往Excel中写新数据 
  44.              */  
  45.             for (int j = 0; j < dataList.size(); j++) {  
  46.                 // 创建一行:从第二行开始,跳过属性列  
  47.                 Row row = sheet.createRow(j + 1);  
  48.                 // 得到要插入的每一条记录  
  49.                 Map dataMap = dataList.get(j);  
  50.                 String name = dataMap.get("BankName").toString();  
  51.                 String address = dataMap.get("Addr").toString();  
  52.                 String phone = dataMap.get("Phone").toString();  
  53.                 for (int k = 0; k <= columnNumCount; k++) {  
  54.                 // 在一行内循环  
  55.                 Cell first = row.createCell(0);  
  56.                 first.setCellValue(name);  
  57.           
  58.                 Cell second = row.createCell(1);  
  59.                 second.setCellValue(address);  
  60.           
  61.                 Cell third = row.createCell(2);  
  62.                 third.setCellValue(phone);  
  63.                 }  
  64.             }  
  65.             // 创建文件输出流,准备输出电子表格:这个必须有,否则你在sheet上做的任何操作都不会有效  
  66.             out =  new FileOutputStream(finalXlsxPath);  
  67.             workBook.write(out);  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         } finally{  
  71.             try {  
  72.                 if(out != null){  
  73.                     out.flush();  
  74.                     out.close();  
  75.                 }  
  76.             } catch (IOException e) {  
  77.                 e.printStackTrace();  
  78.             }  
  79.         }  
  80.         System.out.println("数据导出成功");  
  81.     }  
  82.   
  83.     /** 
  84.      * 判断Excel的版本,获取Workbook 
  85.      * @param in 
  86.      * @param filename 
  87.      * @return 
  88.      * @throws IOException 
  89.      */  
  90.     public static Workbook getWorkbok(File file) throws IOException{  
  91.         Workbook wb = null;  
  92.         FileInputStream in = new FileInputStream(file);  
  93.         if(file.getName().endsWith(EXCEL_XLS)){  //Excel 2003  
  94.             wb = new HSSFWorkbook(in);  
  95.         }else if(file.getName().endsWith(EXCEL_XLSX)){  // Excel 2007/2010  
  96.             wb = new XSSFWorkbook(in);  
  97.         }  
  98.         return wb;  
  99.     }  
  100. }  


猜你喜欢

转载自blog.csdn.net/Jay112011/article/details/80102182