1. 程式人生 > >POI操作excel詳解,HSSF和XSSF兩種方式的區別

POI操作excel詳解,HSSF和XSSF兩種方式的區別

HSSF方式:

[java] view plain copy print?
  1. package com.tools.poi.lesson1;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.text.ParseException;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10. import org.apache.poi.hssf.usermodel.HSSFCell;  
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  12. import org.apache.poi.hssf.usermodel.HSSFRow;  
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  15. import org.apache.poi.hssf.util.HSSFColor;  
  16. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  17. import org.apache.poi.ss.usermodel.Cell;  
  18. import org.apache.poi.ss.usermodel.CellStyle;  
  19. import com.tools.poi.bean.Student;  
  20. publicclass ExcelUtilWithHSSF {  
  21.     publicstaticvoid main(String[] args) {  
  22.         try {  
  23.             getExcelAsFile(”aaa”
    );  
  24.         } catch (FileNotFoundException e) {  
  25.             e.printStackTrace();  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29. //      try {
  30. //          CreateExcelDemo1();
  31. //      } catch (ParseException e) {
  32. //          e.printStackTrace();
  33. //      }
  34.     }  
  35.     /** 
  36.      * 得到Excel,並解析內容 
  37.      * @param file 
  38.      * @throws FileNotFoundException 
  39.      * @throws IOException 
  40.      */
  41.     publicstaticvoid getExcelAsFile(String file) throws FileNotFoundException, IOException{  
  42.         //1.得到Excel常用物件
  43. //      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(“d:/FTP/test.xls”));
  44.         POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(“d:/FTP/new1.xls”));  
  45.         //2.得到Excel工作簿物件
  46.         HSSFWorkbook wb = new HSSFWorkbook(fs);  
  47.         //3.得到Excel工作表物件
  48.         HSSFSheet sheet = wb.getSheetAt(0);  
  49.         //總行數
  50.         int trLength = sheet.getLastRowNum();  
  51.         //4.得到Excel工作表的行
  52.         HSSFRow row = sheet.getRow(0);  
  53.         //總列數
  54.         int tdLength = row.getLastCellNum();  
  55.         //5.得到Excel工作表指定行的單元格
  56.         HSSFCell cell = row.getCell((short)1);  
  57.         //6.得到單元格樣式
  58.         CellStyle cellStyle = cell.getCellStyle();  
  59.         for(int i=0;i<trLength;i++){  
  60.             //得到Excel工作表的行
  61.             HSSFRow row1 = sheet.getRow(i);  
  62.             for(int j=0;j<tdLength;j++){  
  63.             //得到Excel工作表指定行的單元格
  64.             HSSFCell cell1 = row1.getCell(j);  
  65.             /** 
  66.              * 為了處理:Excel異常Cannot get a text value from a numeric cell 
  67.              * 將所有列中的內容都設定成String型別格式 
  68.              */
  69.             if(cell1!=null){  
  70.                   cell1.setCellType(Cell.CELL_TYPE_STRING);  
  71.              }  
  72.             //獲得每一列中的值
  73.             System.out.print(cell1.getStringCellValue()+”\t\t\t”);  
  74.             }  
  75.             System.out.println();  
  76.         }  
  77.     }  
  78.     /** 
  79.      * 建立Excel,並寫入內容 
  80.      */
  81.     publicstaticvoid CreateExcel(){  
  82.         //1.建立Excel工作薄物件
  83.         HSSFWorkbook wb = new HSSFWorkbook();  
  84.         //2.建立Excel工作表物件     
  85.         HSSFSheet sheet = wb.createSheet(”new Sheet”);  
  86.         //3.建立Excel工作表的行   
  87.         HSSFRow row = sheet.createRow(6);  
  88.         //4.建立單元格樣式
  89.         CellStyle cellStyle =wb.createCellStyle();  
  90.           // 設定這些樣式
  91.         cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
  92.         cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  93.         cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  94.         cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  95.         cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  96.         cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  97.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  98.         //5.建立Excel工作表指定行的單元格
  99.         row.createCell(0).setCellStyle(cellStyle);  
  100.         //6.設定Excel工作表的值
  101.         row.createCell(0).setCellValue(“aaaa”);  
  102.         row.createCell(1).setCellStyle(cellStyle);  
  103.         row.createCell(1).setCellValue(“bbbb”);  
  104.         //設定sheet名稱和單元格內容
  105.         wb.setSheetName(0,“第一張工作表”);  
  106.         //設定單元格內容   cell.setCellValue(“單元格內容”);
  107.         // 最後一步,將檔案存到指定位置
  108.                 try
  109.                 {  
  110.                     FileOutputStream fout = new FileOutputStream(“E:/students.xls”);  
  111.                     wb.write(fout);  
  112.                     fout.close();  
  113.                 }  
  114.                 catch (Exception e)  
  115.                 {  
  116.                     e.printStackTrace();  
  117.                 }  
  118.     }  
  119.     /** 
  120.      * 建立Excel的例項 
  121.      * @throws ParseException  
  122.      */
  123.     publicstaticvoid CreateExcelDemo1() throws ParseException{  
  124.         List list = new ArrayList();  
  125.         SimpleDateFormat df = new SimpleDateFormat(“yyyy-mm-dd”);  
  126.         Student user1 = new Student(1“張三”16,true, df.parse(“1997-03-12”));  
  127.         Student user2 = new Student(2“李四”17,true, df.parse(“1996-08-12”));  
  128.         Student user3 = new Student(3“王五”26,false, df.parse(“1985-11-12”));  
  129.         list.add(user1);  
  130.         list.add(user2);  
  131.         list.add(user3);  
  132.         // 第一步,建立一個webbook,對應一個Excel檔案
  133.                 HSSFWorkbook wb = new HSSFWorkbook();  
  134.                 // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet
  135.                 HSSFSheet sheet = wb.createSheet(”學生表一”);  
  136.                 // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short
  137.                 HSSFRow row = sheet.createRow((int0);  
  138.                 // 第四步,建立單元格,並設定值表頭 設定表頭居中
  139.                 HSSFCellStyle style = wb.createCellStyle();  
  140.                 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式
  141.                 HSSFCell cell = row.createCell((short0);  
  142.                 cell.setCellValue(”學號”);  
  143.                 cell.setCellStyle(style);  
  144.                 cell = row.createCell((short1);  
  145.                 cell.setCellValue(”姓名”);  
  146.                 cell.setCellStyle(style);  
  147.                 cell = row.createCell((short2);  
  148.                 cell.setCellValue(”年齡”);  
  149.                 cell.setCellStyle(style);  
  150.                 cell = row.createCell((short3);  
  151.                 cell.setCellValue(”性別”);  
  152.                 cell.setCellStyle(style);  
  153.                 cell = row.createCell((short4);  
  154.                 cell.setCellValue(”生日”);  
  155.                 cell.setCellStyle(style);  
  156.                 // 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,
  157.                 for (int i = 0; i < list.size(); i++)  
  158.                 {  
  159.                     row = sheet.createRow((int) i + 1);  
  160.                     Student stu = (Student) list.get(i);  
  161.                     // 第四步,建立單元格,並設定值
  162.                     row.createCell((short0).setCellValue((double) stu.getId());  
  163.                     row.createCell((short1).setCellValue(stu.getName());  
  164.                     row.createCell((short2).setCellValue((double) stu.getAge());  
  165.                     row.createCell((short)3).setCellValue(stu.getSex()==true?“男”:“女”);  
  166.                     cell = row.createCell((short4);  
  167.                     cell.setCellValue(new SimpleDateFormat(“yyyy-mm-dd”).format(stu  
  168.                             .getBirthday()));  
  169.                 }  
  170.                 // 第六步,將檔案存到指定位置
  171.                 try
  172.                 {  
  173.                     FileOutputStream fout = new FileOutputStream(“E:/students.xls”);  
  174.                     wb.write(fout);  
  175.                     fout.close();  
  176.                 }  
  177.                 catch (Exception e)  
  178.                 {  
  179.                     e.printStackTrace();  
  180.                 }  
  181.     }  
  182. }  
package com.tools.poi.lesson1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

import com.tools.poi.bean.Student;

public class ExcelUtilWithHSSF {
    public static void main(String[] args) {
        try {
            getExcelAsFile("aaa");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


//      try {
//          CreateExcelDemo1();
//      } catch (ParseException e) {
//          e.printStackTrace();
//      }


    }

    /**
     * 得到Excel,並解析內容
     * @param file
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{
        //1.得到Excel常用物件
//      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/test.xls"));
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
        //2.得到Excel工作簿物件
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        //3.得到Excel工作表物件
        HSSFSheet sheet = wb.getSheetAt(0);
        //總行數
        int trLength = sheet.getLastRowNum();
        //4.得到Excel工作表的行
        HSSFRow row = sheet.getRow(0);
        //總列數
        int tdLength = row.getLastCellNum();
        //5.得到Excel工作表指定行的單元格
        HSSFCell cell = row.getCell((short)1);
        //6.得到單元格樣式
        CellStyle cellStyle = cell.getCellStyle();
        for(int i=0;i<trLength;i++){
            //得到Excel工作表的行
            HSSFRow row1 = sheet.getRow(i);
            for(int j=0;j<tdLength;j++){

            //得到Excel工作表指定行的單元格
            HSSFCell cell1 = row1.getCell(j);

            /**
             * 為了處理:Excel異常Cannot get a text value from a numeric cell
             * 將所有列中的內容都設定成String型別格式
             */
            if(cell1!=null){
                  cell1.setCellType(Cell.CELL_TYPE_STRING);
             }

            //獲得每一列中的值
            System.out.print(cell1.getStringCellValue()+"\t\t\t");
            }
            System.out.println();
        }
    }


    /**
     * 建立Excel,並寫入內容
     */
    public static void CreateExcel(){

        //1.建立Excel工作薄物件
        HSSFWorkbook wb = new HSSFWorkbook();
        //2.建立Excel工作表物件     
        HSSFSheet sheet = wb.createSheet("new Sheet");
        //3.建立Excel工作表的行   
        HSSFRow row = sheet.createRow(6);
        //4.建立單元格樣式
        CellStyle cellStyle =wb.createCellStyle();
          // 設定這些樣式
        cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);



        //5.建立Excel工作表指定行的單元格
        row.createCell(0).setCellStyle(cellStyle);
        //6.設定Excel工作表的值
        row.createCell(0).setCellValue("aaaa");

        row.createCell(1).setCellStyle(cellStyle);
        row.createCell(1).setCellValue("bbbb");


        //設定sheet名稱和單元格內容
        wb.setSheetName(0,"第一張工作表");
        //設定單元格內容   cell.setCellValue("單元格內容");

        // 最後一步,將檔案存到指定位置
                try
                {
                    FileOutputStream fout = new FileOutputStream("E:/students.xls");
                    wb.write(fout);
                    fout.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
    }

    /**
     * 建立Excel的例項
     * @throws ParseException 
     */
    public static void CreateExcelDemo1() throws ParseException{
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        Student user1 = new Student(1, "張三", 16,true, df.parse("1997-03-12"));
        Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
        Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
        list.add(user1);
        list.add(user2);
        list.add(user3);


        // 第一步,建立一個webbook,對應一個Excel檔案
                HSSFWorkbook wb = new HSSFWorkbook();
                // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet
                HSSFSheet sheet = wb.createSheet("學生表一");
                // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short
                HSSFRow row = sheet.createRow((int) 0);
                // 第四步,建立單元格,並設定值表頭 設定表頭居中
                HSSFCellStyle style = wb.createCellStyle();
                style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式

                HSSFCell cell = row.createCell((short) 0);
                cell.setCellValue("學號");
                cell.setCellStyle(style);
                cell = row.createCell((short) 1);
                cell.setCellValue("姓名");
                cell.setCellStyle(style);
                cell = row.createCell((short) 2);
                cell.setCellValue("年齡");
                cell.setCellStyle(style);
                cell = row.createCell((short) 3);
                cell.setCellValue("性別");
                cell.setCellStyle(style);
                cell = row.createCell((short) 4);
                cell.setCellValue("生日");
                cell.setCellStyle(style);

                // 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,

                for (int i = 0; i < list.size(); i++)
                {
                    row = sheet.createRow((int) i + 1);
                    Student stu = (Student) list.get(i);
                    // 第四步,建立單元格,並設定值
                    row.createCell((short) 0).setCellValue((double) stu.getId());
                    row.createCell((short) 1).setCellValue(stu.getName());
                    row.createCell((short) 2).setCellValue((double) stu.getAge());
                    row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
                    cell = row.createCell((short) 4);
                    cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
                            .getBirthday()));
                }
                // 第六步,將檔案存到指定位置
                try
                {
                    FileOutputStream fout = new FileOutputStream("E:/students.xls");
                    wb.write(fout);
                    fout.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }



    }
}

XSSF方式: [java] view plain copy print?
  1. package com.tools.poi.lesson1;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.text.ParseException;  
  10. import java.text.SimpleDateFormat;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13. import org.apache.poi.hssf.usermodel.HSSFCell;  
  14. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  15. import org.apache.poi.hssf.usermodel.HSSFRow;  
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  18. import org.apache.poi.hssf.util.HSSFColor;  
  19. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;  
  20. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  21. import org.apache.poi.ss.usermodel.Cell;  
  22. import org.apache.poi.ss.usermodel.CellStyle;  
  23. import org.apache.poi.ss.usermodel.Row;  
  24. import org.apache.poi.ss.usermodel.Sheet;  
  25. import org.apache.poi.ss.usermodel.Workbook;  
  26. import org.apache.poi.ss.usermodel.WorkbookFactory;  
  27. import org.apache.poi.ss.util.WorkbookUtil;  
  28. import com.tools.poi.bean.Student;  
  29. publicclass ExcelUtilWithXSSF {  
  30.     publicstaticvoid main(String[] args) {  
  31.         try {  
  32.             getExcelAsFile(”d:/FTP/系統報表.xls”);  
  33.         } catch (FileNotFoundException e) {  
  34.             e.printStackTrace();  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         } catch (InvalidFormatException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40. //      try {
  41. //          CreateExcelDemo1();
  42. //      } catch (ParseException e) {
  43. //          e.printStackTrace();
  44. //      }
  45.     }  
  46.     /** 
  47.      * 得到Excel,並解析內容  對2007及以上版本 使用XSSF解析 
  48.      * @param file 
  49.      * @throws FileNotFoundException 
  50.      * @throws IOException 
  51.      * @throws InvalidFormatException  
  52.      */
  53.     publicstaticvoid getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{  
  54. //      //1.得到Excel常用物件
  55. //      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(“d:/FTP/new1.xls”));
  56. //      //2.得到Excel工作簿物件
  57. //      HSSFWorkbook wb = new HSSFWorkbook(fs);
  58.         InputStream ins = null;     
  59.         Workbook wb = null;     
  60.             ins=new FileInputStream(new File(file));     
  61.             //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);   
  62.             wb = WorkbookFactory.create(ins);     
  63.             ins.close();     
  64.         //3.得到Excel工作表物件
  65.         Sheet sheet = wb.getSheetAt(0);  
  66.         //總行數
  67.         int trLength = sheet.getLastRowNum();  
  68.         //4.得到Excel工作表的行
  69.         Row row = sheet.getRow(0);  
  70.         //總列數
  71.         int tdLength = row.getLastCellNum();  
  72.         //5.得到Excel工作表指定行的單元格
  73.         Cell cell = row.getCell((short)1);  
  74.         //6.得到單元格樣式
  75.         CellStyle cellStyle = cell.getCellStyle();  
  76.         for(int i=5;i<trLength;i++){  
  77.             //得到Excel工作表的行
  78.             Row row1 = sheet.getRow(i);  
  79.             for(int j=0;j<tdLength;j++){  
  80.             //得到Excel工作表指定行的單元格
  81.             Cell cell1 = row1.getCell(j);  
  82.             /** 
  83.              * 為了處理:Excel異常Cannot get a text value from a numeric cell 
  84.              * 將所有列中的內容都設定成String型別格式 
  85.              */
  86.             if(cell1!=null){  
  87.                   cell1.setCellType(Cell.CELL_TYPE_STRING);  
  88.              }  
  89.             if(j==5&&i<=10){  
  90.                 cell1.setCellValue(”1000”);  
  91.             }  
  92.             //獲得每一列中的值
  93.             System.out.print(cell1+”                   ”);  
  94.             }  
  95.             System.out.println();  
  96.         }  
  97.         //將修改後的資料儲存
  98.         OutputStream out = new FileOutputStream(file);  
  99.                 wb.write(out);  
  100.     }  
  101.     /** 
  102.      * 建立Excel,並寫入內容 
  103.      */
  104.     publicstaticvoid CreateExcel(){  
  105.         //1.建立Excel工作薄物件
  106.         HSSFWorkbook wb = new HSSFWorkbook();  
  107.         //2.建立Excel工作表物件     
  108.         HSSFSheet sheet = wb.createSheet(”new Sheet”);  
  109.         //3.建立Excel工作表的行   
  110.         HSSFRow row = sheet.createRow(6);  
  111.         //4.建立單元格樣式
  112.         CellStyle cellStyle =wb.createCellStyle();  
  113.           // 設定這些樣式
  114.         cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
  115.         cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  116.         cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  117.         cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  118.         cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  119.         cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  120.         cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  121.         //5.建立Excel工作表指定行的單元格
  122.         row.createCell(0).setCellStyle(cellStyle);  
  123.         //6.設定Excel工作表的值
  124.         row.createCell(0).setCellValue(“aaaa”);  
  125.         row.createCell(1).setCellStyle(cellStyle);  
  126.         row.createCell(1).setCellValue(“bbbb”);  
  127.         //設定sheet名稱和單元格內容
  128.         wb.setSheetName(0,“第一張工作表”);  
  129.         //設定單元格內容   cell.setCellValue(“單元格內容”);
  130.         // 最後一步,將檔案存到指定位置
  131.                 try
  132.                 {  
  133.                     FileOutputStream fout = new FileOutputStream(“E:/students.xls”);  
  134.                     wb.write(fout);  
  135.                     fout.close();  
  136.                 }  
  137.                 catch (Exception e)  
  138.                 {  
  139.                     e.printStackTrace();  
  140.                 }  
  141.     }  
  142.     /** 
  143.      * 建立Excel的例項 
  144.      * @throws ParseException  
  145.      */
  146.     publicstaticvoid CreateExcelDemo1() throws ParseException{  
  147.         List list = new ArrayList();  
  148.         SimpleDateFormat df = new SimpleDateFormat(“yyyy-mm-dd”);  
  149.         Student user1 = new Student(1“張三”16,true, df.parse(“1997-03-12”));  
  150.         Student user2 = new Student(2“李四”17,true, df.parse(“1996-08-12”));  
  151.         Student user3 = new Student(3“王五”26,false, df.parse(“1985-11-12”));  
  152.         list.add(user1);  
  153.         list.add(user2);  
  154.         list.add(user3);  
  155.         // 第一步,建立一個webbook,對應一個Excel檔案
  156.                 HSSFWorkbook wb = new HSSFWorkbook();  
  157.                 // 第二步,在webbook中新增一個sheet,對應Excel檔案中的sheet
  158.                 HSSFSheet sheet = wb.createSheet(”學生表一”);  
  159.                 // 第三步,在sheet中新增表頭第0行,注意老版本poi對Excel的行數列數有限制short
  160.                 HSSFRow row = sheet.createRow((int0);  
  161.                 // 第四步,建立單元格,並設定值表頭 設定表頭居中
  162.                 HSSFCellStyle style = wb.createCellStyle();  
  163.                 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式
  164.                 HSSFCell cell = row.createCell((short0);  
  165.                 cell.setCellValue(”學號”);  
  166.                 cell.setCellStyle(style);  
  167.                 cell = row.createCell((short1);  
  168.                 cell.setCellValue(”姓名”);  
  169.                 cell.setCellStyle(style);  
  170.                 cell = row.createCell((short2);  
  171.                 cell.setCellValue(”年齡”);  
  172.                 cell.setCellStyle(style);  
  173.                 cell = row.createCell((short3);  
  174.                 cell.setCellValue(”性別”);  
  175.                 cell.setCellStyle(style);  
  176.                 cell = row.createCell((short4);  
  177.                 cell.setCellValue(”生日”);  
  178.                 cell.setCellStyle(style);  
  179.                 // 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,
  180.                 for (int i = 0; i < list.size(); i++)  
  181.                 {  
  182.                     row = sheet.createRow((int) i + 1);  
  183.                     Student stu = (Student) list.get(i);  
  184.                     // 第四步,建立單元格,並設定值
  185.                     row.createCell((short0).setCellValue((double) stu.getId());  
  186.                     row.createCell((short1).setCellValue(stu.getName());  
  187.                     row.createCell((short2).setCellValue((double) stu.getAge());  
  188.                     row.createCell((short)3).setCellValue(stu.getSex()==true?“男”:“女”);  
  189.                     cell = row.createCell((short4);  
  190.                     cell.setCellValue(new SimpleDateFormat(“yyyy-mm-dd”).format(stu  
  191.                             .getBirthday()));  
  192.                 }  
  193.                 // 第六步,將檔案存到指定位置
  194.                 try
  195.                 {  
  196.                     FileOutputStream fout = new FileOutputStream(“E:/students.xls”);  
  197.                     wb.write(fout);  
  198.                     fout.close();  
  199.                 }  
  200.                 catch (Exception e)  
  201.                 {  
  202.                     e.printStackTrace();  
  203.                 }  
  204.     }  
  205. }  
package com.tools.poi.lesson1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.WorkbookUtil;

import com.tools.poi.bean.Student;

public class ExcelUtilWithXSSF {
    public static void main(String[] args) {
        try {
            getExcelAsFile("d:/FTP/系統報表.xls");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }


//      try {
//          CreateExcelDemo1();
//      } catch (ParseException e) {
//          e.printStackTrace();
//      }


    }

    /**
     * 得到Excel,並解析內容  對2007及以上版本 使用XSSF解析
     * @param file
     * @throws FileNotFoundException
     * @throws IOException
     * @throws InvalidFormatException 
     */
    public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{
//      //1.得到Excel常用物件
//      POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
//      //2.得到Excel工作簿物件
//      HSSFWorkbook wb = new HSSFWorkbook(fs);



        InputStream ins = null;   
        Workbook wb = null;   
            ins=new FileInputStream(new File(file));   
            //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);   
            wb = WorkbookFactory.create(ins);   
            ins.close();   


        //3.得到Excel工作表物件
        Sheet sheet = wb.getSheetAt(0);
        //總行數
        int trLength = sheet.getLastRowNum();
        //4.得到Excel工作表的行
        Row row = sheet.getRow(0);
        //總列數
        int tdLength = row.getLastCellNum();
        //5.得到Excel工作表指定行的單元格
        Cell cell = row.getCell((short)1);
        //6.得到單元格樣式
        CellStyle cellStyle = cell.getCellStyle();

        for(int i=5;i<trLength;i++){
            //得到Excel工作表的行
            Row row1 = sheet.getRow(i);
            for(int j=0;j<tdLength;j++){
            //得到Excel工作表指定行的單元格
            Cell cell1 = row1.getCell(j);
            /**
             * 為了處理:Excel異常Cannot get a text value from a numeric cell
             * 將所有列中的內容都設定成String型別格式
             */
            if(cell1!=null){
                  cell1.setCellType(Cell.CELL_TYPE_STRING);
             }

            if(j==5&&i<=10){
                cell1.setCellValue("1000");
            }

            //獲得每一列中的值
            System.out.print(cell1+"                   ");
            }
            System.out.println();
        }

        //將修改後的資料儲存
        OutputStream out = new FileOutputStream(file);
                wb.write(out);
    }


    /**
     * 建立Excel,並寫入內容
     */
    public static void CreateExcel(){

        //1.建立Excel工作薄物件
        HSSFWorkbook wb = new HSSFWorkbook();
        //2.建立Excel工作表物件     
        HSSFSheet sheet = wb.createSheet("new Sheet");
        //3.建立Excel工作表的行   
        HSSFRow row = sheet.createRow(6);
        //4.建立單元格樣式
        CellStyle cellStyle =wb.createCellStyle();
          // 設定這些樣式
        cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);



        //5.建立Excel工作表指定行的單元格
        row.createCell(0).setCellStyle(cellStyle);
        //6.設定Excel工作表的值
        row.createCell(0).setCellValue("aaaa");

        row.createCell(1).setCellStyle(cellStyle);
        row.createCell(1).setCellValue("bbbb");


        //設定sheet名稱和單元格內容
        wb.setSheetName(0,"第一張工作表");
        //設定單元格內容   cell.setCellValue("單元格內容");

        // 最後一步,將檔案存到指定位置
                try
                {
                    FileOutputStream fout = new FileOutputStream("E:/students.xls");
                    wb.write(fout);
                    fout.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
    }

    /**
     * 建立Excel的例項
     * @throws ParseException 
     */
    public static void CreateExcelDemo1() throws ParseException{
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        Student user1 = new Student(1, "張三", 16,true, df.parse("1997-03-12"));
        Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
        Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
        list.add(user1);
        list.add(user2);
        list.add(user3);