本章將介紹如何建立一個電子試算表,並使用Java操縱它。電子試算表是在Excel檔案中的頁面;它包含具有特定名稱的行和列。
讀完本章後,將能夠建立一個電子試算表,並能在其上執行讀取操作。
首先,讓我們建立一個使用在前面的章節中討論的參照的類的電子試算表。按照前面的章節中,首先建立一個工作簿,然後我們就可以去,並建立一個表。
下面的程式碼片段用於建立電子試算表。
//Create Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank spreadsheet XSSFSheet spreadsheet = workbook.createSheet("Sheet Name");
電子試算表有一個網格布局。行和列被標識與特定的名稱。該列標識字母和行用數位。
下面的程式碼片段用於建立一個行。
XSSFRow row = spreadsheet.createRow((short)1);
讓我們考慮雇員資料的一個例子。這裡的雇員資料給出以表格形式。
Emp Id | Emp Name | 稱號 |
---|---|---|
Tp01 | Gopal | Technical Manager |
TP02 | Manisha | Proof Reader |
Tp03 | Masthan | Technical Writer |
Tp04 | Satish | Technical Writer |
Tp05 | Krishna | Technical Writer |
以下程式碼是用來寫上述資料到電子試算表。
import java.io.File; import java.io.FileOutputStream; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Writesheet { public static void main(String[] args) throws Exception { //Create blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet spreadsheet = workbook.createSheet( " Employee Info "); //Create row object XSSFRow row; //This data needs to be written (Object[]) Map < String, Object[] > empinfo = new TreeMap < String, Object[] >(); empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" }); empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" }); empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" }); empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" }); empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" }); empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" }); //Iterate over data and write to sheet Set < String > keyid = empinfo.keySet(); int rowid = 0; for (String key : keyid) { row = spreadsheet.createRow(rowid++); Object [] objectArr = empinfo.get(key); int cellid = 0; for (Object obj : objectArr) { Cell cell = row.createCell(cellid++); cell.setCellValue((String)obj); } } //Write the workbook in file system FileOutputStream out = new FileOutputStream( new File("Writesheet.xlsx")); workbook.write(out); out.close(); System.out.println( "Writesheet.xlsx written successfully" ); } }
上面的Java程式碼儲存為Writesheet.java,然後並在命令提示字元下編譯執行,如下所示:
$javac Writesheet.java $java Writesheet
這將編譯和執行來生成一個Excel檔案名為Writesheet.xlsx在當前目錄中,在命令提示字元處鍵入以下輸出。
Writesheet.xlsx written successfully
Writesheet.xlsx檔案的內容如下所示。
讓我們考慮上述excel檔案命名Writesheet.xslx作為輸入檔案。注意下面的程式碼;它是用於從電子試算表中讀取資料。
import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Readsheet { static XSSFRow row; public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream( new File("WriteSheet.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet spreadsheet = workbook.getSheetAt(0); Iterator < Row > rowIterator = spreadsheet.iterator(); while (rowIterator.hasNext()) { row = (XSSFRow) rowIterator.next(); Iterator < Cell > cellIterator = row.cellIterator(); while ( cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: System.out.print( cell.getNumericCellValue() + " \t\t " ); break; case Cell.CELL_TYPE_STRING: System.out.print( cell.getStringCellValue() + " \t\t " ); break; } } System.out.println(); } fis.close(); } }
讓我們把上面的程式碼儲存在Readsheet.java檔案,然後編譯並在命令提示字元下執行,如下所示:
$javac Readsheet.java $java Readsheet
如果您的系統環境組態了POI庫,它會編譯和執行產生在命令提示字元處鍵入以下輸出。
EMP ID EMP NAME DESIGNATION tp01 Gopal Technical Manager tp02 Manisha Proof Reader tp03 Masthan Technical Writer tp04 Satish Technical Writer tp05 Krishna Technical Writer