poi创建excel文件
package com.mozq.sb.file01.test;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Excel_01 {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("创建sheet页");
//默认的列宽度8字符
System.out.println(sheet.getDefaultColumnWidth());
//设置默认列宽和默认行高
sheet.setDefaultColumnWidth(20);
sheet.setDefaultRowHeightInPoints((short) 20);
//存储最大列宽
Map<Integer,Integer> maxWidth = new HashMap<>();
maxWidth.put(0, getLength("错误行"));
maxWidth.put(1,getLength("Excel表格导入错误消息"));
for (Integer key : maxWidth.keySet()) {
System.out.println(key + ":" + maxWidth.get(key));
sheet.setColumnWidth(key, maxWidth.get(key));
}
HSSFRow sheetTitle = sheet.createRow(1);
//创建
HSSFRow row = sheet.createRow(2);//创建行
//设置行高
row.setHeightInPoints(30);
HSSFCell cell = row.createCell(0);
cell.setCellValue("错误行");
cell.setCellStyle(getTitleCellStyle(workbook));//设置样式
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue("Excel表格导入错误消息");
cell1.setCellStyle(getTitleCellStyle(workbook));//设置样式
//-------------打印内容
List<String[]> content = new ArrayList<>();
content.add(new String[]{"第1行", "电话号码格式错误"});
content.add(new String[]{"第2行", "用户信息不存在"});
for (int i = 0; i < content.size(); i++) {
HSSFRow dataRow = sheet.createRow(i + 3);
String[] strs = content.get(i);
for (int j = 0; j < strs.length; j++) {
HSSFCell posCell = dataRow.createCell(j);
posCell.setCellValue(strs[j]);
posCell.setCellStyle(getTextCellStyle(workbook));
System.out.println(strs[j]);
}
}
FileOutputStream fileOutputStream=new FileOutputStream("E:\\00\\3.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
public static Integer getLength(String content){
Integer length = 0;
if(Objects.nonNull(content)){
length = content.getBytes().length * 2 * 230;
}
return length;
}
//创建标题栏通用样式
public static HSSFCellStyle getTitleCellStyle(HSSFWorkbook workbook){
/* 对齐方式 */
HSSFCellStyle cellStyle= workbook.createCellStyle(); //设置样式
//水平居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//垂直居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
/* 边框设置 */
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
/* 字体样式 */
HSSFFont font = workbook.createFont();
//字体颜色
font.setColor(HSSFColor.DARK_BLUE.index);
//字体高度
font.setFontHeightInPoints((short) 14);
//字体
font.setFontName("宋体");
//加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
return cellStyle;
}
//创建标题栏通用样式
public static HSSFCellStyle getTextCellStyle(HSSFWorkbook workbook){
/* 对齐方式 */
HSSFCellStyle cellStyle= workbook.createCellStyle(); //设置样式
//水平居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//垂直居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
/* 边框设置 */
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
/* 字体样式 */
HSSFFont font = workbook.createFont();
//字体颜色
font.setColor(HSSFColor.BLACK.index);
//字体高度
font.setFontHeightInPoints((short) 14);
//字体
font.setFontName("宋体");
//字体加粗
//font.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
return cellStyle;
}
}
poi创建excel文件的更多相关文章
- Java Struts2 POI创建Excel文件并实现文件下载
Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...
- java使用poi创建excel文件
import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import or ...
- Java通过poi创建Excel文件并分页追加数据
以下的main函数,先生成一个excel文件,并设置sheet的名称,设置excel头:而后,以分页的方式,向文件中追加数据 maven依赖 <dependency> <groupI ...
- 简单的poi导出excel文件
/**** 创建excel文件**/ 1 import java.io.FileOutputStream; import java.io.IOException; import java.util.C ...
- java使用Apache POI操作excel文件
官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...
- POI生成EXCEL文件
POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...
- java使用jxl,poi解析excel文件
public interface JavaExcel { /** * 使用jxl写excel文件 */ public void writeJxlExcel(); /** * 使用jxl读excel文件 ...
- 使用poi读写excel文件
使用poi库测试了一下读取excel文件,效果不错,跟大家分享一下. 第一列是数值型,第二列是字符型,代码如下: package poi; import java.io.FileInputStream ...
- java通过poi编写excel文件
public String writeExcel(List<MedicalWhiteList> MedicalWhiteList) { if(MedicalWhiteList == nul ...
随机推荐
- Python的面试题
(1)怎么把一个字符串转换成整型? 可以使用int函数 如 int('3') 结果由字符串'3'变为整型3 (2)python内建数据类型有哪些? int .bool. str.list. ru ...
- Newman
目录 简介 安装 使用 简介 Newman是为postman而生,专门用来运行postman编写好的脚本 使用Newman,你可以很方便的用命令行来执行postman collections Newm ...
- WPF之图片处理系列
WPF 中的一些图片处理方法 一,视觉处理(控件展示) 1,显示图片 Image控件展示 Xaml代码: <Image source="/Resources/Images/1.png& ...
- THLM,CSS
目录 HTTP协议的四大特性 数据格式 状态码 HTML概念 标签 标签分类 按是否封闭分类 按级别分类 标签属性 head内常用标签 body内常用标签 body内重要标签 a 标签 img 标签 ...
- cd ..、cd / 和 cd ~ 的区别
cd ..是回到上一级目录 cd . 是当前目录 cd / 是回到根目录 cd ~ 回到用户主目录
- 《Java练习题》习题集一
编程合集: https://www.cnblogs.com/jssj/p/12002760.html Java总结:https://www.cnblogs.com/jssj/p/11146205.ht ...
- JavaFX如何为按钮设置快捷键?
JavaFX为按钮设置快捷键的方式有很多,先说下常见的一种. 第一种: KeyCodeCombination kc1 = new KeyCodeCombination(KeyCode.W, KeyCo ...
- Java生鲜电商平台-生鲜系统中商品订单系统售后系统设计
Java生鲜电商平台-生鲜系统中商品订单系统售后系统设计(服务订单履约系统) 说明: 电商之下,我们几乎能从电商平台上买到任何我们日常需要的商品,但是对于很多商品来说,用户购买发货后,只是整个交易流程 ...
- Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例
场景 Nginx入门简介和反向代理.负载均衡.动静分离理解 https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ub ...
- VueCLi3 配置less变量
Step1. 文档介绍 // vue-cli css预处理文档: https://cli.vuejs.org/zh/guide/css.html#自动化导入 // less文档: https://ww ...