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 ...
随机推荐
- linux-iptables增、删、改、保存
iptables基础: iptables的5条链分别是: prerouting 路由前 input 发到本机进程的报文 ouput 本机某进程发出的报文 forword 转发 postrouting ...
- Python3 猜年龄小游戏进阶之函数处理
在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理 登录函数 注册函数 猜年龄函数 选择奖品函数 # 注册 def register(): '''注册''' count = 0 while ...
- Day 05 文本处理和爬虫基础1
目录 什么是文件 什么是文本 如何通过文本编辑器控制.txt文件 打开文件的三种模式 t和b模式 高级应用 文本处理 + 词云分析 效果如下 爬虫原理 requests模块 re模块 爬取图片 爬取视 ...
- Win32_PhysicalMedia 硬盘 参数说明
Caption 物理内存还虚拟内存 Description 描述和Caption一样 InstallDate 安装日期(无值) Name 名字 Status 状态 CreationClassName ...
- django----cookie与session 和 中间件
目录 cookie与session简介及操作 cookie django中操作cookie cookie超时时间 删除cookie session session操作 设置 key value发生了什 ...
- webpack学习_使用source map
追踪错误和警告,JS提供sourcemap功能,将编译后的代码映射回原始代码(简单来说就是即使打包后,也可以检测知道该错误来自哪个JS文件).如果一个错误来自与b.js,那么source map回明确 ...
- CCF-CSP题解 201609-3 炉石传说
模拟. 注意随从的编号在\(summon\)和\(attack\)随从死亡时都可能改变. #include <bits/stdc++.h> using namespace std; str ...
- 基于TCP协议之SSH
#SSH客户端 import socket # 1. 创建符合TCp协议的手机 client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # ...
- 防止DataGridview闪烁
在Load事件中加入 typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | Bi ...
- Caffe源码-Net类(下)
net.cpp部分源码 // 接着上一篇博客的介绍,此部分为Net类中前向反向计算函数,以及一些与HDF5文件或proto文件相互转换的函数. template <typename Dtype& ...