POI(2)-----------簡單操作Excel表

1、簡單創建單元格

/ * HFFSWorkbook讀寫xls格式
		 * XFFSWorkbook讀寫xlsx
		 * SXSSFWorkbook低內存佔用,超過65536條數據使用
		 */
		//創建一個新的工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		FileOutputStream fos = new FileOutputStream(new File("1.xls"));
		wb.write(fos);
		fos.close();
		System.out.println("create wb successfully");
		
		//打開現有的工作薄
		File file = new File("1.xls");
		FileInputStream fis = new FileInputStream(file);
		HSSFWorkbook hsf = new HSSFWorkbook(fis);
		if(file.isFile() && file.exists()){
			System.out.println("1.xls存在,並打開");
		}else{
			System.out.println("1.xl打開失敗!");		
		}


2、插入數據

                /*
		 * ID     NAME    部門
		 * t1     Tom      01
		 * t2     Jim      02
		 * t3     Lock     03
		 */
		//創建一個sheet工作區
		HSSFWorkbook hwb = new HSSFWorkbook();
		HSSFSheet hs = hwb.createSheet("部門");
		HSSFRow row;
		Map<String,Object[]> map = new TreeMap<String,Object[]>();
		map.put("1",new Object[]{"ID","Name","部門"});
		map.put("2",new Object[]{"t1","Tom","01"});
		map.put("3",new Object[]{"t2","Jim","02"});
		map.put("4",new Object[]{"t3","Lock","03"});
		Set<String> sets = map.keySet();
		int rowId = 0;
		for (String key : sets) {
			row = hs.createRow(rowId++);
			Object[] obj = map.get(key);
			int cellId = 0;
			for(Object o : obj){
				Cell cell = row.createCell(cellId++);
				cell.setCellValue((String)o);
			}
		}
		
		FileOutputStream out = new FileOutputStream(new File("1.xls"));
		hwb.write(out);
		out.close();
		System.out.println("successfully");
		

3、單元格設置


//單元格樣式
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("cellStyle");
		HSSFRow row = sheet.createRow((short)2);
		
		row.setHeight((short)800);//設置高度    第2行  ,行高度800 ,下標1單元格添加“蘇其昌”“黃志強” 合併區域1行4列
		HSSFCell cell = row.createCell(1);
		
		cell.setCellValue("昌哥");
		cell.setCellValue("強哥");
		sheet.addMergedRegion(new CellRangeAddress(
			1,//first row
			1,//last row
			1,//first column
			4 //last column
				));
		row = sheet.createRow(4);   //第4行,下標0的單元格,高度100,寬度0-8000,左對齊,居上
		cell = row.createCell(0);
		row.setHeight((short)100);
		sheet.setColumnWidth(0, 8000);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
		cell.setCellValue("top--->left");
		cell.setCellStyle(style);
		//第6行  下標2的單元格  高度800,樣式 居中、
		row = sheet.createRow(6);
		cell = row.createCell(1);
		row.setHeight((short)800);
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cell.setCellStyle(style2);
		cell.setCellValue("101");
		
		row = sheet.createRow(10);
		row.setHeight((short)800);
		cell = row.createCell(2);
		HSSFCellStyle style3 = wb.createCellStyle();
		style3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
		cell.setCellStyle(style3);
		cell.setCellValue("sanqing");
      row = sheet.createRow((short) 10);
	      row.setHeight((short) 800);
	      cell =  row.createCell( 1);
	      cell.setCellValue("BORDER");
	      HSSFCellStyle style5 = wb.createCellStyle();
	      style5.setBorderBottom(HSSFCellStyle.BORDER_THICK);
	      style5.setBottomBorderColor(
	      IndexedColors.BLUE.getIndex());
	      style5.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
	      style5.setLeftBorderColor( 
	      IndexedColors.GREEN.getIndex());
	      style5.setBorderRight(HSSFCellStyle.BORDER_HAIR);
	      style5.setRightBorderColor( 
	      IndexedColors.RED.getIndex());
	      style5.setBorderTop(HSSFCellStyle.BIG_SPOTS);
	      style5.setTopBorderColor( 
	      IndexedColors.CORAL.getIndex());
	      cell.setCellStyle(style5);
	      
	      FileOutputStream fos = new FileOutputStream("2.xls");
	      wb.write(fos);
	      fos.close();


4、字體設置

                HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("s1");
		HSSFRow row = sheet.createRow(10);
		row.setHeight((short)1500);
		HSSFCell cell = row.createCell(2);
		cell.setCellValue("河南大學");
		//設置字體樣式
		HSSFFont font = wb.createFont();
		font.setColor(HSSFColor.BRIGHT_GREEN.index);
		font.setFontName("IMPACT");
		font.setItalic(true);
		font.setFontHeight((short)500);
		
		//設置邊框的樣式
		HSSFCellStyle s = wb.createCellStyle();
		s.setBorderBottom((short)100);
		s.setBorderTop(HSSFCellStyle.VERTICAL_TOP);
		//旋轉字體
		s.setRotation((short)90);
		//將字體樣式加入,樣式
		s.setFont(font);
		cell.setCellStyle(s);
		
		
		FileOutputStream fos = new FileOutputStream("3.xls");
		wb.write(fos);
		fos.close();

5、函數計算

                //1、創建一個HSSFWorkbook對象
		HSSFWorkbook wb = new HSSFWorkbook();
		//2、創建一個工作薄HSSFSheet
		HSSFSheet sheet = wb.createSheet("我的工作薄");
		//3、創建行第5行
		HSSFRow row = sheet.createRow((short)5);
		//4、創建工作單元格
		HSSFCell cell = row.createCell(1);
		cell.setCellValue("A=");
		cell = row.createCell(2);
		cell.setCellValue(2);
		
		//創建行第6行
		row = sheet.createRow((short)6);
		cell = row.createCell(1);
		cell.setCellValue("C=");
		cell = row.createCell(2);
		cell.setCellValue(2);
		//創建行第七行
		row = sheet.createRow((short)7);
		cell = row.createCell(1);
		cell.setCellValue("Total=");
		cell = row.createCell(2);
		//創建求和函數
		cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
		cell.setCellFormula("SUM(c6:c7)");
		cell.setCellValue(2);
		//同理創建 power、max、pact、sqrt等
		
		row = sheet.createRow(10);
		cell = row.createCell(3);
		//設置字體
		HSSFFont font = wb.createFont();
		font.setColor(HSSFColor.AQUA.index);
		font.setBoldweight((short)500);
		font.setFontHeight((short)500);
		font.setFontName("微軟雅黑");
		//設置樣式
		HSSFCellStyle style = wb.createCellStyle();
		style.setFont(font);
		style.setFillBackgroundColor(HSSFColor.YELLOW.index);
		style.setLocked(true);	
		cell.setCellStyle(style);
		
		cell.setCellValue("https://www.yiibai.com");
		
		OutputStream os = new FileOutputStream("5.xls");
		wb.write(os);
		os.close();
		

6、打印設置

                HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("101");
		wb.setPrintArea(0, 0,5,0,5);
		sheet.getPrintSetup().setPageStart(HSSFPrintSetup.A4_PAPERSIZE);
		//set display grid lines or not
		sheet.setDisplayGridlines(true);
		//set print grid lines or not
		sheet.setPrintGridlines(true);
		
		OutputStream os = new FileOutputStream("6.xls");
		wb.write(os);
		os.close();
		//打印預覽查看

7、總結:

1、先創建一個HSSFWorkbook對象

2、創建Sheet工作表

3、創建Row

4、創建單元格Cell

5、創建字體設置HSSFFont對象

6、創建HSSFCellStyle對象

7、將font加入style

8、創建一個OutputStream流對象

9、寫入表

10、關閉流對象



發佈了49 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章