POI的使用 [JAVA_Apache]POI套件的使用_產出EXCEL文件_產出EXCEL方法(Workbook)

           
public static void training1() throws IOException {
 FileOutputStream fileOut = null ;
  try {
   //這裡只是純產出excel檔案而以
   //產出Excel 97(XLS)文件格式
   Workbook hssfwb = new HSSFWorkbook();
   fileOut = new FileOutputStream("workbook.xls");
   hssfwb.write(fileOut);
   fileOut.close();
   //Excel 2007的(XLSX)文件格式
   Workbook xssfwb = new XSSFWorkbook();
   fileOut = new FileOutputStream("workbook.xlsx");
   xssfwb.write(fileOut);
   fileOut.close();
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
  }
  finally {
   if (null != fileOut) {
    fileOut.close();
   }
  }
 }

 
public static void training2() throws IOException {
  // 讀取一個原有的excel檔案
  FileInputStream fileInputStream = null;
  Workbook hssfWorkbook = null;
  try {
   fileInputStream = new FileInputStream(new File("D:/training1.xls"));
   if (null != fileInputStream) {
    try {
     hssfWorkbook = new HSSFWorkbook(fileInputStream);
    }
    catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
   throw ioe;
  }
  finally {
   if (null != fileInputStream) {
    fileInputStream.close();
   }
  }
  //將剛讀取的excel檔案,產出為另一個excel檔案。
  FileOutputStream fileOutputStream = null;
  try {
   fileOutputStream = new FileOutputStream(new File("D:/trainingto.xls"));
   hssfWorkbook.write(fileOutputStream);
   fileOutputStream.flush();
  }
  catch (IOException ioe) {
   ioe.printStackTrace();
   throw ioe;
  }
  finally {
   if (null != fileOutputStream) {
    fileOutputStream.close();
   }
  }
 }

其它文章

 

文章標籤
全站熱搜
創作者介紹
創作者 PG Levin Li 的頭像
PG Levin Li

程式開發學習之路

PG Levin Li 發表在 痞客邦 留言(0) 人氣(216)