如何在 Java 中读写 Excel 文件

x33g5p2x  于9个月前 转载在 Java  
字(3.4k)|赞(0)|评价(0)|浏览(88)

在本教程中,我们将使用 JExcel API 解释 How to Read and Write Excel file in Java。 让我们一步一步地看看我们首先需要创建一个 Excel Workbook,下一步是创建 Sheet 并将内容添加到工作表中。

用于创建工作簿的代码片段

WritableWorkbook writableWorkbook = null;
writableWorkbook = Workbook.createWorkbook(new File("WebSparrow.xls"));

阅读工作簿的代码片段

Workbook wb = null;
wb = Workbook.getWorkbook(new File("WebSparrow.xls"));

必需的 jars

1.jxl.jar

下载这个 jar 文件 click here。 下载 JAR 文件后,将其添加到项目库中。

####写入Excel文件

用于创建新 Excel 文件的代码。
WriteExcelExp.java

package org.websparrow;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class WriteExcelExp {
	public static void main(String[] args) {
		WritableWorkbook writableWorkbook = null;
		try {
			// create file name
			writableWorkbook = Workbook.createWorkbook(new File("WebSparrow.xls"));

			// create sheet name
			WritableSheet writableSheet = writableWorkbook.createSheet("EmployeeList", 0);

			// adding font
			WritableCellFormat writableCellFormat = new WritableCellFormat();
			WritableFont writableFont = new WritableFont(WritableFont.TAHOMA, 10, WritableFont.BOLD);
			writableCellFormat.setFont(writableFont);

			// adding content into sheet
			Label lbl = new Label(0, 0, "Emp ID", writableCellFormat);
			writableSheet.addCell(lbl);

			lbl = new Label(1, 0, "Emp Name", writableCellFormat);
			writableSheet.addCell(lbl);

			lbl = new Label(2, 0, "Designation", writableCellFormat);
			writableSheet.addCell(lbl);

			// for first emp
			Number num = new Number(0, 1, 1);
			writableSheet.addCell(num);

			lbl = new Label(1, 1, "Atul Rai");
			writableSheet.addCell(lbl);

			lbl = new Label(2, 1, "CEO");
			writableSheet.addCell(lbl);
			// for second emp
			num = new Number(0, 2, 2);
			writableSheet.addCell(num);

			lbl = new Label(1, 2, "Sandeep Sharma");
			writableSheet.addCell(lbl);

			lbl = new Label(2, 2, "CTO");
			writableSheet.addCell(lbl);
			
			// write all above in to workbook and close
			writableWorkbook.write();
			writableWorkbook.close();
			System.out.println("Done");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

输出:

编译并运行上述代码后,您将获得以下输出。 要检查输出,请转到您的文件位置并打开生成的 Excel 文件。

####读取Excel文件

读取上述 Excel 文件的代码。
阅读ExcelExp.java

package org.websparrow;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadExcelExp {
	public static void main(String[] args) {
		Workbook wb = null;
		try {
			wb = Workbook.getWorkbook(new File("WebSparrow.xls"));
			Sheet sheet = wb.getSheet(0);

			Cell c1 = sheet.getCell(0, 0);
			Cell c3 = sheet.getCell(1, 0);
			Cell c5 = sheet.getCell(2, 0);
			System.out.println(c1.getContents() + "  " + c3.getContents() + "         " + c5.getContents());

			Cell c2 = sheet.getCell(0, 1);
			Cell c4 = sheet.getCell(1, 1);
			Cell c6 = sheet.getCell(2, 1);
			System.out.println(c2.getContents() + "       " + c4.getContents() + "            " + c6.getContents());

			Cell c7 = sheet.getCell(0, 2);
			Cell c8 = sheet.getCell(1, 2);
			Cell c9 = sheet.getCell(2, 2);
			System.out.println(c7.getContents() + "       " + c8.getContents() + "      " + c9.getContents());
			
			// for simple use the below code
			/*
			  Cell c1 = sheet.getCell(0,0);
			  System.out.print(c1.getContents()+" "); Cell c2=
			  sheet.getCell(0,1); System.out.println(c2.getContents());
			  
			  Cell c3 = sheet.getCell(1,0);
			  System.out.print(c3.getContents()+" "); Cell c4=
			  sheet.getCell(1,1); System.out.println(c4.getContents());
			  
			  Cell c5 = sheet.getCell(2,0);
			  System.out.print(c5.getContents()+" "); Cell c6=
			  sheet.getCell(2,1); System.out.println(c6.getContents());
			 */

			wb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

输出:

Emp ID  Emp Name         Designation
1       Atul Rai            CEO
2       Sandeep Sharma      CTO

相关文章

热门文章

更多