一个比较经典的POI小例子

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

public class POI_MultiStruct {

	public static void main(String...args){
		POI_MultiStruct poi = new POI_MultiStruct();
		poi.doTest();
	}
	
	public void doTest(){
		try{
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, "測試頁");
			HSSFCellStyle normalStyle = createNormalStyle(wb);
//			sheet.addMergedRegion(new Region(0,(short)0,0,(short)2));
			sheet.addMergedRegion(new CellRangeAddress(0,0,0,2));//firstRow,lastRow,firstCol,lastCol
			HSSFRow row = sheet.createRow(0);
			HSSFCell cell = row.createCell(0);
			cell.setCellValue("業務一覽表");
			cell.setCellStyle(normalStyle);
			row.createCell(1).setCellStyle(normalStyle);
			row.createCell(2).setCellStyle(normalStyle);
			String[] seasonName = {"第一季度","第二季度","每三季度","第四季度"};
			for(int i=3;i<7;i++){
				cell = row.createCell(i);
				cell.setCellValue(seasonName[i-3]);
				cell.setCellStyle(normalStyle);
				
			}
			
			for(int i=1;i<7;i++){//從1到6    創建行
				sheet.createRow(i).createCell(0).setCellStyle(normalStyle);
			}
			
			HSSFPatriarch patr = sheet.createDrawingPatriarch();
			HSSFComment comment = patr.createCellComment(new HSSFClientAnchor(0,0,0,0,(short)4,8,(short)7,12));
			comment.setString(new HSSFRichTextString("POI行列順序太亂了吧!\n一會兒行在前,一會兒列在前"));
			
			sheet.addMergedRegion(new CellRangeAddress(1,6,(short)0,(short)0));
			row = sheet.createRow(1);
			cell = row.createCell(0);
			cell.setCellValue("地\n區\n代\n理");
//			System.out.println(cell);
//			cell.setCellValue(new HSSFRichTextString("地\n區\n代\n理\n"));
//			sheet.setColumnWidth(0, 200);
			cell.setCellComment(comment);
			cell.setCellStyle(normalStyle);
			
			
			String[] shopName = {"連鎖店A","連鎖店B","連鎖店C"};
			for(int i=1;i<6;i+=2){
				row = sheet.getRow(i);
				sheet.getRow(i+1).createCell(1).setCellStyle(normalStyle);
				sheet.addMergedRegion(new CellRangeAddress(i,i+1,(short)1,(short)1));
				cell = row.createCell(1);
				cell.setCellValue(shopName[(i-1)/2]);
				cell.setCellStyle(normalStyle);
				
			}
			
			for(int i=1;i<7;i+=2){
				cell = sheet.getRow(i).createCell(2);
				cell.setCellValue("收入");
				cell.setCellStyle(normalStyle);
				cell = sheet.getRow(i+1).createCell(2);
				cell.setCellValue("支出");
				cell.setCellStyle(normalStyle);
			}
			
			for(int i=1;i<7;i++){
				for(int j=3;j<7;j++){
					cell = sheet.getRow(i).createCell(j);
					cell.setCellValue(i*j);
					cell.setCellStyle(normalStyle);
				}
			}
				
				sheet.createFreezePane(3, 1);
				
				for(int i=1;i<7;i+=2){
					sheet.groupRow(i,i);
				}
				
				sheet.groupRow(1,5);
				
				FileOutputStream writeFile = new FileOutputStream("D:\\test.xls");
				wb.write(writeFile);
				writeFile.flush();
				writeFile.close();
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	public static HSSFCellStyle createNormalStyle(HSSFWorkbook wb){
		HSSFCellStyle normalStyle = wb.createCellStyle();
		normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		normalStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		normalStyle.setTopBorderColor(HSSFColor.BLACK.index);
		normalStyle.setRightBorderColor(HSSFColor.BLACK.index);
		normalStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		normalStyle.setWrapText(true);
		return normalStyle;
	}
}


参考:http://blog.csdn.net/youjianbo_han_87/archive/2010/06/08/5655988.aspx

你可能感兴趣的:(apache,.net,Blog,J#)