java技术总结|POI3.9测试

引入
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import javax.imageio.ImageIO;


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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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;

【java技术总结|POI3.9测试】

测试代码

public class MyPoi { public void testExcel() { HSSFWorkbook excelfile = new HSSFWorkbook(); HSSFSheet excelsheet = excelfile.createSheet(); excelfile.setSheetName(0, "呵呵"); //合并单元格样式 HSSFCellStyle styleTitle = excelfile.createCellStyle(); HSSFFont fnt = excelfile.createFont(); fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); fnt.setFontHeightInPoints((short)16); styleTitle.setFont(fnt); styleTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleTitle.setFillForegroundColor(HSSFColor.ORANGE.index); styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION); //列名单元格样式 HSSFCellStyle styleHead = excelfile.createCellStyle(); styleHead.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleHead.setFillForegroundColor(HSSFColor.BLUE_GREY.index); styleHead.setBorderBottom(HSSFCellStyle.BORDER_THIN); styleHead.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION); HSSFFont fnt2 = excelfile.createFont(); fnt2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); fnt2.setFontHeightInPoints((short)16); styleHead.setFont(fnt2); //内容单元格样式 HSSFCellStyle styleBody = excelfile.createCellStyle(); styleBody.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleBody.setFillForegroundColor(HSSFColor.LAVENDER.index); styleBody.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION); for (int i = 0; i < 10; i++) { HSSFRow row = excelsheet.createRow(i); row.setHeight((short)400); for (int j = 0; j < 5; j++) { HSSFCell c = row.createCell(j); if (i == 1) { c.setCellStyle(styleHead); c.setCellValue(" " + i + j); } else { c.setCellValue(" " + i + j); c.setCellStyle(styleBody); } } }//合并单元格 excelsheet.addMergedRegion(new CellRangeAddress(0,0,0,4)); excelsheet.getNumMergedRegions(); excelsheet.getRow(0).getCell(0).setCellStyle(styleTitle); excelsheet.getRow(0).getCell(0).setCellValue("不符合要求的"); try {FileOutputStream fileOut = new FileOutputStream("E:/1/t" + System.currentTimeMillis() + ".xls"); excelfile.write(fileOut); fileOut.close(); } catch (IOException e) { System.out.println(e.getLocalizedMessage()); } } public void testChart() { } public static void main(String[] args) { new MyPoi().testExcel(); }}

效果图

    推荐阅读