1.poi导出excel
/**
* 2003格式
*/
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class poi
{
public static void main(String[] args) throws IOException
{
// 新建工作表对象
HSSFWorkbook book = new HSSFWorkbook();
// 自定义sheet
Sheet sheet = book.createSheet("自定义1");
//创建行
Row row = sheet.createRow(0);
//创建单元格
Cell cell = row.createCell(0);
cell.setCellValue("单元格测试值");
HSSFCellStyle cellStyle = book.createCellStyle();
cellStyle.setFillForegroundColor((short) 13);// 设置背景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//设置居中:
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
//设置字体:
HSSFFont font = book.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小
HSSFFont font2 = book.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font2.setFontHeightInPoints((short) 12);
// 使字体生效
cellStyle.setFont(font);//选择需要用到的字体格式
// 使样式生效
cell.setCellStyle(cellStyle);
//构建文件
FileOutputStream fileout = new FileOutputStream("D://test.xls");
// 将工作表兑现写入文件中
book.write(fileout);
}
}
响应结果:
依赖jar包:
demo:
链接:https://pan.baidu.com/s/1jkgclbfsMbDP0f38wGcI4A
提取码:5i8n