Java的poi技术读取和导入Excel

转载自:http://developer.51cto.com/art/201202/319070.htm


    1.  /**  
    2.      * 读取xls文件内容  
    3.      *  
    4.      * @return List<XlsDto>对象  
    5.      * @throws IOException  
    6.      *             输入/输出(i/o)异常  
    7.      */ 
    8.     private List<XlsDto> readXls() throws IOException {  
    9.         InputStream is = new FileInputStream("pldrxkxxmb.xls");  
    10.         HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);  
    11.         XlsDto xlsDto = null;  
    12.         List<XlsDto> list = new ArrayList<XlsDto>();  
    13.         // 循环工作表Sheet  
    14.         for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {  
    15.             HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);  
    16.             if (hssfSheet == null) {  
    17.                 continue;  
    18.             }  
    19.             // 循环行Row  
    20.             for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {  
    21.                 HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
    22.                 if (hssfRow == null) {  
    23.                     continue;  
    24.                 }  
    25.                 xlsDto = new XlsDto();  
    26.                 // 循环列Cell  
    27.                 // 0学号 1姓名 2学院 3课程名 4 成绩  
    28.                 // for (int cellNum = 0; cellNum <=4; cellNum++) {  
    29.                 HSSFCell xh = hssfRow.getCell(0);  
    30.                 if (xh == null) {  
    31.                     continue;  
    32.                 }  
    33.                 xlsDto.setXh(getValue(xh));  
    34.                 HSSFCell xm = hssfRow.getCell(1);  
    35.                 if (xm == null) {  
    36.                     continue;  
    37.                 }  
    38.                 xlsDto.setXm(getValue(xm));  
    39.                 HSSFCell yxsmc = hssfRow.getCell(2);  
    40.                 if (yxsmc == null) {  
    41.                     continue;  
    42.                 }  
    43.                 xlsDto.setYxsmc(getValue(yxsmc));  
    44.                 HSSFCell kcm = hssfRow.getCell(3);  
    45.                 if (kcm == null) {  
    46.                     continue;  
    47.                 }  
    48.                 xlsDto.setKcm(getValue(kcm));  
    49.                 HSSFCell cj = hssfRow.getCell(4);  
    50.                 if (cj == null) {  
    51.                     continue;  
    52.                 }  
    53.                 xlsDto.setCj(Float.parseFloat(getValue(cj)));  
    54.                 list.add(xlsDto);  
    55.             }  
    56.         }  
    57.         return list;  
    58.     }  


XlsDto2Excel.java类

//该类主要负责向Excel(2003版)中插入数据

 
  1. import java.io.FileOutputStream;  
  2. import java.io.OutputStream;  
  3. import java.util.List;  
  4.    
  5. import org.apache.poi.hssf.usermodel.HSSFCell;  
  6. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  7. import org.apache.poi.hssf.usermodel.HSSFRow;  
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10.    
  11.    
  12. public class XlsDto2Excel {  
  13.    
  14.     /**  
  15.      *  
  16.      * @param xls  
  17.      *            XlsDto实体类的一个对象  
  18.      * @throws Exception  
  19.      *             在导入Excel的过程中抛出异常  
  20.      */ 
  21.     public static void xlsDto2Excel(List<XlsDto> xls) throws Exception {  
  22.         // 获取总列数  
  23.         int CountColumnNum = xls.size();  
  24.         // 创建Excel文档  
  25.         HSSFWorkbook hwb = new HSSFWorkbook();  
  26.         XlsDto xlsDto = null;  
  27.         // sheet 对应一个工作页  
  28.         HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");  
  29.         HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始  
  30.         HSSFCell[] firstcell = new HSSFCell[CountColumnNum];  
  31.         String[] names = new String[CountColumnNum];  
  32.         names[0] = "学号";  
  33.         names[1] = "姓名";  
  34.         names[2] = "学院";  
  35.         names[3] = "课程名";  
  36.         names[4] = "成绩";  
  37.         for (int j = 0; j < CountColumnNum; j++) {  
  38.             firstcell[j] = firstrow.createCell(j);  
  39.             firstcell[j].setCellValue(new HSSFRichTextString(names[j]));  
  40.         }  
  41.         for (int i = 0; i < xls.size(); i++) {  
  42.             // 创建一行  
  43.             HSSFRow row = sheet.createRow(i + 1);  
  44.             // 得到要插入的每一条记录  
  45.             xlsDto = xls.get(i);  
  46.             for (int colu = 0; colu <= 4; colu++) {  
  47.                 // 在一行内循环  
  48.                 HSSFCell xh = row.createCell(0);  
  49.                 xh.setCellValue(xlsDto.getXh());  
  50.                 HSSFCell xm = row.createCell(1);  
  51.                 xm.setCellValue(xlsDto.getXm());  
  52.                 HSSFCell yxsmc = row.createCell(2);  
  53.                 yxsmc.setCellValue(xlsDto.getYxsmc());  
  54.                 HSSFCell kcm = row.createCell(3);  
  55.                 kcm.setCellValue(xlsDto.getKcm());  
  56.                 HSSFCell cj = row.createCell(4);  
  57.                 cj.setCellValue(xlsDto.getCj());  
  58. (xlsDto.getMessage());  
  59.             }  
  60.         }  
  61.         // 创建文件输出流,准备输出电子表格  
  62.         OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");  
  63.         hwb.write(out);  
  64.         out.close();  
  65.         System.out.println("数据库导出成功");  
  66.     }  
  67.    
  68. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值