TestExprot
package excel;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress; public class TestExprot { public static void main(String[] args) {
List<User> users = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
users.add(new User(i, "桔梗" + i, "冰岛" + i, i % 2, new Date()));
}
String path = "D:/kikyo.xls";
export(users, path);
} /**
* 导出
*
* @param users 数据
* @param path 保存路径
*/
public static void export(List<User> users, String path) {
// 1,创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 2,在工作簿里面创建sheet
// workbook.createSheet()//只是创建名字为默认的sheetX
HSSFSheet sheet = workbook.createSheet("用户数据");
// 3,sheet的相关设置
// sheet.setColumnHidden(columnIndex, hidden);设置某一列是否隐藏
// sheet.setColumnWidth(1, 20*256);//设置某一列的宽度
sheet.setDefaultColumnWidth(25);// 设置默认列度
// sheet.setDefaultRowHeight((short) (20 * 20));// 设置默认行高 // 合并
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 4);
sheet.addMergedRegion(region);
CellRangeAddress region2 = new CellRangeAddress(1, 1, 0, 4);
sheet.addMergedRegion(region2); int row = 0;
// 在sheet里面创建行
HSSFRow row1 = sheet.createRow(row);
// 在这一行里面创建一个单元格
HSSFCell row1_cell1 = row1.createCell(0);
// 向row1_cell1里面添加数据
row1_cell1.setCellValue("用户数据");
//创建标题样式
HSSFCellStyle titleStyle = createTitleStyle(workbook);
row1_cell1.setCellStyle(titleStyle); // 第二行
row++;
HSSFRow row2 = sheet.createRow(row);
HSSFCell row2_cell1 = row2.createCell(0);
//创建小标题样式
HSSFCellStyle subTitleStyle = createSubTitleStyle(workbook);
row2_cell1.setCellValue("总条数:" + users.size() + " 导出时间:" + new Date().toLocaleString());
row2_cell1.setCellStyle(subTitleStyle);
// 第三行
String[] titles = {"用户ID", "用户名", "用户地址", "性别", "入职时间"};
row++;
HSSFRow row3 = sheet.createRow(row);
//创建表头样式
HSSFCellStyle tableHeaderStyle = createTableTitleStyle(workbook);
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = row3.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(tableHeaderStyle);
}
//创建基础样式
HSSFCellStyle baseStyle = createBaseStyle(workbook);
//第四行到最后
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
row++;
HSSFRow rowx = sheet.createRow(row); HSSFCell cell1 = rowx.createCell(0);
cell1.setCellValue(user.getId());
cell1.setCellStyle(baseStyle); HSSFCell cell2 = rowx.createCell(1);
cell2.setCellValue(user.getName());
cell2.setCellStyle(baseStyle); HSSFCell cell3 = rowx.createCell(2);
cell3.setCellValue(user.getAddress());
cell3.setCellStyle(baseStyle); HSSFCell cell4 = rowx.createCell(3);
cell4.setCellValue(user.getSex() == 1 ? "男" : "女");
cell4.setCellStyle(baseStyle); HSSFCell cell5 = rowx.createCell(4);
// cell5.setCellValue(user.getBirth().toLocaleString());
String birth = DateFormat.getDateTimeInstance().format(user.getBirth());
cell5.setCellValue(birth);
cell5.setCellStyle(baseStyle);
}
// 导出保存到D盘
try {
workbook.write(new File(path));
System.out.println("导出成功");
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 创建基础样式
* 水平和垂直居中
*/
public static HSSFCellStyle createBaseStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = workbook.createCellStyle();
//设置水平居中
style.setAlignment(HorizontalAlignment.CENTER);
//设置垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
return style;
} /**
* 创建数据表格的头的样式
*/
public static HSSFCellStyle createTableTitleStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = createBaseStyle(workbook); //设置字体
HSSFFont font = workbook.createFont();
font.setBold(true);//是否加粗
font.setItalic(true);//是否斜体
font.setFontHeightInPoints((short) 25); //设置字体大小
font.setColor(HSSFColor.HSSFColorPredefined.DARK_YELLOW.getIndex());//设置颜色
font.setFontName("华文行楷");//设置字体
style.setFont(font); return style;
} /**
* 创建小标题样式
*/
public static HSSFCellStyle createSubTitleStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = createBaseStyle(workbook);
//设置字体
HSSFFont font = workbook.createFont();
font.setBold(true);//是否加粗
font.setFontHeightInPoints((short) 18); //设置字体大小
font.setColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());//设置颜色
font.setFontName("黑体");//设置字体
style.setFont(font);
return style;
} /**
* 创建标题样式
*/
public static HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = createBaseStyle(workbook);
//设置字体
HSSFFont font = workbook.createFont();
font.setBold(true);//是否加粗
font.setFontHeightInPoints((short) 35); //设置字体大小
font.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());//设置颜色
font.setFontName("华文彩云");//设置字体
style.setFont(font);
return style;
}
}
User
package excel;

import java.util.Date;

public class User {
private Integer id;
private String name;
private String address;
private Integer sex;
private Date birth; public User() {
// TODO Auto-generated constructor stub
} public User(Integer id, String name, String address, Integer sex, Date birth) {
super();
this.id = id;
this.name = name;
this.address = address;
this.sex = sex;
this.birth = birth;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public Integer getSex() {
return sex;
} public void setSex(Integer sex) {
this.sex = sex;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
}
}

pox.xml

    <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>

【Java excel】导出excel文件的更多相关文章

  1. java 实现导出Excel文件

    java 实现导出Excel(java生成 excel 并导出文件) 经常有有一些数据需要导出成   excel  格式 ,所以就需要实现啦 开始: 1.加入jar poi-3.6-20091214. ...

  2. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

  3. java后端导出excel

    最近工作中需要导出excel.这次机智一点做个笔记,顺便写了一个比较通用的工具类.自然目前不能生成java实体类属性嵌套多次的这种没办法导出了,后续有需要的时候我再改改. 首先,java后端导出exc ...

  4. thinkphp3.2.3 excel导出,下载文件,包含图片

    关于导出后出错的问题 https://segmentfault.com/q/1010000005330214 https://blog.csdn.net/ohmygirl/article/detail ...

  5. Java POI导出Excel不弹框选择下载路径(下载文件不选择下载路径,默认) Chrome

    在Chrome浏览器中,Java导出Excel文件时,浏览器弹出提示框,需要选择下载路径 在Chrome中的高级设置中,把“下载前询问每个文件的保存位置”去掉就解决了 DEEPLOVE(LC)

  6. java POI导出Excel文件数据库的数据

    在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...

  7. java - 读取,导出 excel文件数据

    首先需下载poi java包,添加至构建路径, 写处理方法: import java.io.FileInputStream;import java.io.FileOutputStream;import ...

  8. java poi导出EXCEL xls文件代码

    String _currentPage = request.getParameter("currentPage"); Integer currentPage = 0; if(_cu ...

  9. java:POI导出excel

    POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...

  10. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

随机推荐

  1. Unity小知识点大全(二)

    51.Inspector调试模式 在Inspector面板右上角的下拉菜单中,选择Debug命令,启动调试模式,此时将显示组件包含的所有变量,包括私有变量,当运行编辑器时,可以实时查看各组件所有变量的 ...

  2. 使用Idea构建springmvc框架,出现no bean named 'cacheManager' is defined 错误

    由于IDEA的自动补全功能非常强大,当你配置 <mvc:annotation-driven/> 后编译器会帮你自动补全上面两个配置文件约束.这个时候如果你没注意的就会爆出一个很莫名奇妙的错 ...

  3. ubuntu刪除軟件

    1.打开一个终端,输入dpkg --list ,按下Enter键,终端输出以下内容,显示的是你电脑上安装的所有软件2.在终端中找到你需要卸载的软件的名称,列表是按照首字母排序的.3.在终端上输入命令s ...

  4. import matplotlib.pyplot as plt出错

    >>>import matplotlib.pyplot as plt /usr/lib/python2.7/dist-packages/matplotlib/font_manager ...

  5. Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students

    You are the gym teacher in the school. There are nn students in the row. And there are two rivalling ...

  6. 2.5 【配置环境】多浏览器驱动 (chrome、IE、Firefox)❀

    来源:http://blog.csdn.net/huilan_same/article/details/51896672 http://www.cnblogs.com/thinkCoding/p/64 ...

  7. 主席树 hdu 4417

    求一个区间内小于等于limit的数: 主席树模板题. 求出每一个节点的sum: #include<cstdio> #include<algorithm> #include< ...

  8. shell批量创建数据表的一个方法

    #!/bin/bash #批量新建数据表 #删除`符号,具体原因我也没搞懂 for i in {1..30};do mysql 地址 -u账号 -p密码 -e "use 库名;CREATE ...

  9. Java - Java 命令行简介: 选项, 属性, 参数

    概述 简单介绍一下 java 命令行相关的参数及属性 1. java 命令行 基本 命令 > java <mainClass> 描述 执行 Java 类 需要准备好编译完成的 mai ...

  10. 架构师必备技能指南:SaaS(软件即服务)架构设计

    1.介绍 从计算机诞生开始,就伴随着计算机应用程序的演变.简短的回顾历史,我们可以清楚的看到应用程序发生的巨大变化.上世纪70年代中期,随着个人PC机的爆炸式增长以及程序员的崛起,让计算机的计算能力得 ...