java poi

  1. package com.poi.second;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.util.Date;  
  5.   
  6. import org.apache.poi.hssf.usermodel.HSSFCell;  
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  8. import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
  9. import org.apache.poi.hssf.usermodel.HSSFRow;  
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  12. import org.apache.poi.ss.usermodel.CreationHelper;  
  13.   
  14. /** 
  15.  * 写excel 
  16.  * @author 钟林森 
  17.  * 
  18.  */  
  19. public class ExcelWrite {  
  20.     public static void main(String[] args) throws Exception{  
  21.           
  22.         //创建一个工作簿 即excel文件,再在该文件中创建一个sheet  
  23.         HSSFWorkbook wb=new HSSFWorkbook();  
  24.         HSSFSheet sheet=wb.createSheet("第一个sheet");  
  25.           
  26.         //在sheet中创建一行  
  27.         HSSFRow row=sheet.createRow(0);  
  28.           
  29.         //在该行写入各种类型的数据  
  30.         row.createCell(0).setCellValue(true);  
  31.         row.createCell(1).setCellValue("钟林森");  
  32.         row.createCell(2).setCellValue(23);  
  33.           
  34.         //设置保留两位小数  
  35.         HSSFCell cell=row.createCell(3);  
  36.             cell.setCellValue(6000);  
  37.                 HSSFCellStyle cellStyle = wb.createCellStyle();  
  38.                 cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));  
  39.                 cell.setCellStyle(cellStyle);  
  40.           
  41.         //在写入 日期格式的 数据需要进行特殊处理(这是一种 简单的处理方式)  
  42.         CreationHelper createHelper=wb.getCreationHelper();  
  43.         HSSFCellStyle style=wb.createCellStyle();  
  44.         style.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));  
  45.           
  46.         cell=row.createCell(4);  
  47.         cell.setCellValue(new Date());  
  48.         cell.setCellStyle(style);  
  49.           
  50.         //最后写回磁盘  
  51.         FileOutputStream out=new FileOutputStream("E:\\java_BasicWeb\\someFiles\\excel写数据.xls");  
  52.         wb.write(out);  
  53.         out.close();  
  54.           
  55.         System.out.println("写完了!");  
  56.     }  
  1. }  
  1. package com.poi.second;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.InputStream;  
  5. import java.text.SimpleDateFormat;  
  6.   
  7. import org.apache.poi.hssf.usermodel.HSSFCell;  
  8. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  9. import org.apache.poi.hssf.usermodel.HSSFRow;  
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  13.   
  14. /** 
  15.  * 读excel 
  16.  * @author 钟林森 
  17.  * 
  18.  */  
  19. public class ExcelRead {  
  20.     public static void main(String[] args) throws Exception {  
  21.   
  22.         //读取一个excel表的内容  
  23.         InputStream stream = new FileInputStream("E:\\java_BasicWeb\\someFiles\\excel读数据.xls");  
  24.         POIFSFileSystem fs = new POIFSFileSystem(stream);  
  25.         HSSFWorkbook wb = new HSSFWorkbook(fs);  
  26.   
  27.         //获取excel表的第一个sheet  
  28.         HSSFSheet sheet = wb.getSheetAt(0);  
  29.         if (sheet == null) {  
  30.             return;  
  31.         }  
  32.   
  33.         //遍历该sheet的行  
  34.         for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {  
  35.             HSSFRow row = sheet.getRow(rowNum);  
  36.             if (row == null) {  
  37.                 continue;  
  38.             }  
  39.   
  40.             //再遍历改行的所有列  
  41.             for(int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {  
  42.                 HSSFCell cell = row.getCell(cellNum);  
  43.                 if (cell == null) {  
  44.                     continue;  
  45.                 }  
  46.                   
  47.                   
  48.                 String strVal=readCellSecondMethod(cell);  
  49.                 if (cellNum==2) {  
  50.                     strVal=strVal.contains(".")?strVal.substring(0, strVal.indexOf(".")):strVal;  
  51.                 }  
  52.                 System.out.print(" " + strVal);  
  53.                   
  54.                 //System.out.print(" " + readCellFirstMethod(cell));  
  55.                 //System.out.print(" " + readCellSecondMethod(cell));  
  56.                   
  57.             }  
  58.             System.out.println();  
  59.         }  
  60.   
  61.         stream.close();  
  62.     }  
  63.   
  64.     /**第一种方法 
  65.      * 读取excel单元格的内容并针对其type进行不同的处理, 
  66.      * 其中就包含  读取excel表格中日期格式的cell 
  67.      * @param cell 
  68.      * @return 
  69.      */  
  70.     public static String readCellFirstMethod(HSSFCell cell) {  
  71.         if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {  
  72.             return String.valueOf(cell.getBooleanCellValue());  
  73.         } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
  74.             if (HSSFDateUtil.isCellDateFormatted(cell)) {  
  75.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  76.                 return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();  
  77.             }  
  78.             return String.valueOf(cell.getNumericCellValue());  
  79.         } else {  
  80.             return cell.getStringCellValue();  
  81.         }  
  82.     }  
  83.   
  84.     /**第二种方法 
  85.      * 读取excel单元格的内容并针对其type进行不同的处理, 
  86.      * 其中就包含  读取excel表格中日期格式的cell 
  87.      * @param cell 
  88.      * @return 
  89.      */  
  90.     public static String readCellSecondMethod(HSSFCell cell) {  
  91.         //DecimalFormat df = new DecimalFormat("#");  
  92.         if (cell == null) {  
  93.             return "";  
  94.         }  
  95.         switch (cell.getCellType()) {  
  96.           
  97.             //数字  
  98.             case HSSFCell.CELL_TYPE_NUMERIC:  
  99.                   
  100.                 //日期格式的处理  
  101.                 if (HSSFDateUtil.isCellDateFormatted(cell)) {  
  102.                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  103.                     return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();  
  104.                 }  
  105.                   
  106.                 return String.valueOf(cell.getNumericCellValue());  
  107.                 //return df.format(cell.getNumericCellValue());  
  108.                   
  109.             //字符串  
  110.             case HSSFCell.CELL_TYPE_STRING:  
  111.                 return cell.getStringCellValue();  
  112.               
  113.             //公式  
  114.             case HSSFCell.CELL_TYPE_FORMULA:  
  115.                 return cell.getCellFormula();  
  116.                   
  117.             //空白  
  118.             case HSSFCell.CELL_TYPE_BLANK:  
  119.                 return "";  
  120.               
  121.             //布尔取值  
  122.             case HSSFCell.CELL_TYPE_BOOLEAN:  
  123.                 return cell.getBooleanCellValue() + "";  
  124.               
  125.             //错误类型  
  126.             case HSSFCell.CELL_TYPE_ERROR:  
  127.                 return cell.getErrorCellValue() + "";  
  128.         }  
  129.           
  130.         return "";  
  131.     }  
  132.   
  1. }  
  2.  

猜你喜欢

转载自blog.csdn.net/zhangDXR/article/details/79959327