引入
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();
}}
效果图
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)