1. 程式人生 > >Apache POI 簡單使用示例程式碼

Apache POI 簡單使用示例程式碼

Apache POI 使用詳解

示例程式碼:

/**
 * poi 操作excel例項
 *
 */
public class PoiTest {

	public static void main(String args[]) throws IOException {
		
		PoiTest test = new PoiTest();
		
		//建立表
		//test.create();
		
		//讀取表
		test.read();
	}
	
	/**
	 * 建立excel檔案,寫操作
	 * @throws IOException
	 */
	public void create() throws IOException{
		
		String filePath="d:\\sample.xls";//檔案路徑
		HSSFWorkbook workbook = new HSSFWorkbook();//建立Excel檔案(Workbook)
		HSSFSheet sheet = workbook.createSheet("Test2");//建立工作表(Sheet)
		HSSFRow row = sheet.createRow(0);// 建立行,從0開始
		HSSFCell cell = row.createCell(0);// 建立行的單元格,也是從0開始
		cell.setCellValue("ocean");// 設定單元格內容
		row.createCell(1).setCellValue("kwkw");
		row.createCell(2).setCellValue("dsfdfdf");
		
		//設定日期格式--使用Excel內嵌的格式
		HSSFCell cell3=row.createCell(3);
		cell3.setCellValue(new Date());
		HSSFCellStyle style3=workbook.createCellStyle();
		style3.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		cell3.setCellStyle(style3);
		
		//設定保留2位小數--使用Excel內嵌的格式
		HSSFCell cell4=row.createCell(4);
		cell4.setCellValue(12.3456789);
		HSSFCellStyle style4=workbook.createCellStyle();
		style4.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
		cell4.setCellStyle(style4);
		
		//設定中文大寫格式--使用自定義的格式
		HSSFCell cell5=row.createCell(5);
		cell5.setCellValue(12345);
		HSSFCellStyle style5=workbook.createCellStyle();
		style5.setDataFormat(workbook.createDataFormat().getFormat("[DbNum2][$-804]0"));
		cell5.setCellStyle(style5);
		
		//和並列
		CellRangeAddress region=new CellRangeAddress(0, 0, 1, 2);//CellRangeAddress(firstRow, lastRow, firstCol, lastCol)
		sheet.addMergedRegion(region);
		
		//合併行
		HSSFCell cell6=row.createCell(6);
		cell6.setCellValue("合併行");
		CellRangeAddress region2=new CellRangeAddress(0, 1, 6, 6);//CellRangeAddress(firstRow, lastRow, firstCol, lastCol)
		sheet.addMergedRegion(region2);
		
		//單元格對其方式
		HSSFCellStyle style6=workbook.createCellStyle();
		style6.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
		cell6.setCellStyle(style6);
		
		//使用邊框
		cell=row.createCell(7);
		cell.setCellValue("設定邊框");
		HSSFCellStyle style7=workbook.createCellStyle();
		style7.setBorderTop(HSSFCellStyle.BORDER_DOTTED);//上邊框
		style7.setBorderBottom(HSSFCellStyle.BORDER_THICK);//下邊框
		style7.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);//左邊框
		style7.setBorderRight(HSSFCellStyle.BORDER_SLANTED_DASH_DOT);//右邊框
		style7.setTopBorderColor(HSSFColor.RED.index);//上邊框顏色
		style7.setBottomBorderColor(HSSFColor.BLUE.index);//下邊框顏色
		style7.setLeftBorderColor(HSSFColor.GREEN.index);//左邊框顏色
		style7.setRightBorderColor(HSSFColor.PINK.index);//右邊框顏色
		cell.setCellStyle(style7);
		
		//設定寬度和高度
		sheet.setColumnWidth(0, 5*256);//設定第0列的寬度是15個字元寬度
		row.setHeightInPoints(50);//設定第0行的高度是50個點
		
		//輸出表
		FileOutputStream out = new FileOutputStream(filePath);
		workbook.write(out);//儲存Excel檔案
		out.close();//關閉檔案流
		System.out.println("OK!");
	}
	
	/**
	 * 讀取excel檔案,讀操作
	 * @throws IOException
	 */
	public void read() throws IOException{
		
		String filePath="d:\\sample.xls";//檔案路徑
		FileInputStream stream = new FileInputStream(filePath);
		HSSFWorkbook workbook = new HSSFWorkbook(stream);//讀取現有的Excel		
		HSSFSheet sheet = workbook.getSheetAt(0);//得到第一個sheet
		
		for(Row row : sheet){
			for(org.apache.poi.ss.usermodel.Cell cell: row){
				System.out.println(cell+"\t");
			}
			System.out.println();
		}
	}
		
}