- Apache POI Tutorial
- Apache POI - Home
- Apache POI - Overview
- Apache POI - Java Excel APIs
- Apache POI - Environment
- Apache POI - Core Classes
- Apache POI - Workbooks
- Apache POI - Spreadsheets
- Apache POI - Cells
- Apache POI - Fonts
- Apache POI - Formula
- Apache POI - Hyperlink
- Apache POI - Print Area
- Apache POI - Database
- Apache POI Resources
- Apache POI - Questions & Answers
- Apache POI - Quick Guide
- Apache POI - Useful Resources
- Apache POI - Discussion
Apache POI - 电子表格
本章介绍如何使用 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);
写入电子表格
让我们考虑一个员工数据的例子。这里,员工数据以表格形式给出。
员工 ID | 员工姓名 | 指定 |
---|---|---|
TP01 | 戈帕尔 | 技术经理 |
TP02 | 马尼沙 | 校对读者 |
TP03 | 马斯坦 | 技术文件撰稿人 |
TP04 | 萨蒂什 | 技术文件撰稿人 |
TP05 | 克里希纳 | 技术文件撰稿人 |
以下代码用于将上述数据写入电子表格中。
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
它将编译并执行以在当前目录中生成名为Writesheet.xlsx的 Excel 文件,并且您将在命令提示符中获得以下输出。
Writesheet.xlsx written successfully
Writesheet.xlsx文件如下所示-
从电子表格中读取
让我们将上面名为Writesheet.xslx的 Excel 文件视为输入。遵守以下代码;它用于从电子表格中读取数据。
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 NUMERIC: System.out.print(cell.getNumericCellValue() + " \t\t "); break; case 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