1. 程式人生 > >POI操作EXCEL,追加或覆蓋資料,輸入輸出流注意事項,jar包的選擇

POI操作EXCEL,追加或覆蓋資料,輸入輸出流注意事項,jar包的選擇

首先有兩種資料格式,如果只是為用表格,那麼用xls格式就夠用了。但如果你還用得到XML的東西,那麼需要用xlsx這個格式。

實現程式碼很簡單。

2017.9.22補充:

1.一定要在XSSFWorkbook用輸入流當成建構函式引數建立新物件後,再使用輸入流。如果直接先把輸入流,輸出流建立好了以後,再建立新物件,就會報錯。錯誤資訊為: org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file。但是我並不想知道這是為什麼了,因為我只想避開這個問題。

錯誤程式碼順序如下(假設你已經新建了一個Excel檔案):

		    File excel=new File("F:\\build.xlsx");
		    FileInputStream fs;
		    fs=new FileInputStream(excel);
		    FileOutputStream out=new FileOutputStream(excel);
		    XSSFWorkbook workbook = new XSSFWorkbook(fs);

正確程式碼順序如下:
                    File excel=new File("F:\\build.xlsx");
		    FileInputStream fs;
		    fs=new FileInputStream(excel);		    
		    XSSFWorkbook workbook = new XSSFWorkbook(fs);
		    FileOutputStream out=new FileOutputStream(excel);
2.輸入流,輸出流在使用之後(輸入流用於建構函式,輸出流用於寫入資料),再使用同樣的流做之前的事會報錯(再使用輸入流報錯流為空,再使用輸出流報錯為 org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /docProps/app.xml failed to be saved in the stream with marshaller org.[email protected]4bbfb90a)。所以對於同一個引用的流,在使用之後還想要繼續使用的話,必須重新新建一個流物件。

輸入流被使用情況:

FileInputStream fs=new FileInputStream(excel);
XSSFWorkbook workbook = new XSSFWorkbook(fs);
輸出流被使用情況:
FileOutputStream out=new FileOutputStream(excel);
workbook.write(out);
3.XSSFSheet,這個物件想要獲得資料,必須是先獲得行,再獲得這行的某一個單元。即必須先行後列。即沒有直接獲得列資料的方法。

對於獲得行資料,有兩種方法createRow和getRow。顧名思義,前者是新建行,後者是獲得行。createRow是不管原來這一行原來是不是有原始資料,都會新建一行。getRow是獲得原始行資料,如果本來就是空行,那麼就獲得一個空行。

對於獲得單元資料,有兩種方法createCell和getCell。功能與上述內容類似。

所以,當我們想要追加資料,或者是想根據原來的內容修改資料,就應該使用get方法。而想要使用新的資料來直接覆蓋掉舊的資料時,就應該使用create方法。



package trust;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 

import dataStru.user;

public class JExcel {

	 public static void Export(LinkedHashMap<String,Integer> map) throws FileNotFoundException, IOException
	 {
		 HSSFWorkbook wb = new HSSFWorkbook();
		 HSSFSheet sheet = wb.createSheet("table");  //建立table工作薄
		 
		 HSSFRow row1,row2;
		 row1 = sheet.createRow(0);
		 row2 = sheet.createRow(1);
		 HSSFCell cell1,cell2;
		 int count=0;
		 for(Entry<String, Integer> a:map.entrySet())
		 {
			 cell1 = row1.createCell(count);
			 cell2 = row2.createCell(count);
			 cell1.setCellValue(a.getKey());
			 cell2.setCellValue(a.getValue());
			 count++;
		 }
		
		 
		 wb.write(new FileOutputStream("E:\\table.xls"));
	 }
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		ArrayList<user> userList=read.readtext();
		int[] biao=statistics.countFor(userList, 0.1, 0, 3);
		LinkedHashMap<String,Integer> map=statistics.qujian(biao, 10);//自己定義區間大小
		JExcel.Export(map);
	}

}
重點講下poi-bin-3.16-20170419這個你去apace官網下的api檔案,考慮到我實現的功能特別簡單,在新增jar包時,各位可能還需要另外再新增jar包,但我這些肯定都是必須的。

先放下這個檔案解壓後的目錄結構,


首先如果是使用xls這個老格式的EXCEL,那麼就只需要poi-3.16.jar這一個jar包,我想一般工程都夠用了吧

而如果是使用xlsx這個新格式的EXCEL,那麼就還需要幾個,如下圖所示


還有就是程式碼的改動就只是把上面的HSSF全部變成XSSF就行。