生成简单的Excel表格示例
其他
2018-06-05 12:58:15
阅读次数: 2
package com.sanhai.nep.managerService.util;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* Created by 胥源博 on 2016/1/26.
*/
public class Test {
public static void main(String[] args) throws Exception {
arexcelUtil arexcelUtil = new arexcelUtil();
List list = new ArrayList();
list.add(new AccessRecord("id","111","222","333",":","444","555","666","777","888"));
arexcelUtil.genarateExcel(list);
}
}
class arexcelUtil {
private ResourceBundle bundle = ResourceBundle.getBundle("exportTable");//得到exportTable.properties文件
private static WritableCellFormat wcf_value; // 表格数据样式
private static WritableCellFormat wcf_value_left;
private static WritableCellFormat wcf_key; // 表头样式
private static WritableCellFormat wcf_name_left; // 表名样式
private static WritableCellFormat wcf_name_right; // 表名样式
private static WritableCellFormat wcf_name_center; // 表名样式
private static WritableCellFormat wcf_title; // 页名称样式
private static WritableCellFormat wcf_percent_float;
//根据exportTable.properties文件里的key得到Excel表格的路径
private static String pathStr = bundle.getString("exportRableRoute");
// 生成Excel文件
public void genarateExcel(List list) throws Exception {
/****** 定义表格格式start *****/
WritableFont wf_key = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
WritableFont wf_value = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);
//设置单元格样式
wcf_value = new WritableCellFormat(wf_value); //单元格字体样式
wcf_value.setAlignment(jxl.format.Alignment.CENTRE); //单元格水平对齐样式
wcf_value.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //单元格垂直对齐样式
wcf_value.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边框样式
wcf_value_left = new WritableCellFormat(wf_value);
wcf_value_left.setAlignment(jxl.format.Alignment.LEFT);
wcf_value_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_value_left.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
wcf_value_left.setWrap(true);
wcf_key = new WritableCellFormat(wf_key);
wcf_key.setAlignment(jxl.format.Alignment.CENTRE);
wcf_key.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
wcf_name_left = new WritableCellFormat(wf_key);
wcf_name_left.setAlignment(Alignment.LEFT);
wcf_name_left.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_name_right = new WritableCellFormat(wf_key);
wcf_name_right.setAlignment(Alignment.LEFT);
wcf_name_center = new WritableCellFormat(wf_key);
wcf_name_center.setAlignment(Alignment.CENTRE);
jxl.write.NumberFormat wf_percent_float = new jxl.write.NumberFormat("0.00"); //定义单元浮点数据类型
wcf_percent_float = new jxl.write.WritableCellFormat(wf_value, wf_percent_float);
wcf_percent_float.setAlignment(jxl.format.Alignment.CENTRE);
wcf_percent_float.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_percent_float.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
WritableFont wf_title = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 24, WritableFont.NO_BOLD); //定义标题样式
wcf_title = new WritableCellFormat(wf_title);
wcf_title.setAlignment(Alignment.CENTRE);
wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
/****** 定义表格格式end *****/
//在指定的路径生成空白的xls文件
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println();
String filename = pathStr + df.format(new Date()) + ".xls";
File file = new File(filename);
file.createNewFile();
//根据传输过来的list,定义excel表格的列数
//int maxRow = list.size();
//int maxCol = 10;
Iterator it = list.iterator();
try {
WritableWorkbook wb = Workbook.createWorkbook(file);
//设置Excel工作簿名称
WritableSheet ws = wb.createSheet("访问报表", 0);
int startRowNum = 0; // 起始行
int startColNum = 0; // 起始列
int maxColSize = 8; // 最大列数
// 设置列宽
ws.setColumnView(0, 10);
ws.setColumnView(1, 18);
ws.setColumnView(2, 12);
ws.setColumnView(3, 12);
ws.setColumnView(4, 12);
ws.setColumnView(5, 28);
ws.setColumnView(6, 28);
ws.setColumnView(7, 15);
ws.addCell(new Label(startColNum, startRowNum, "来访单位记录", wcf_title));
ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize - 1, startRowNum); //合并单元格,合并(1,0)到(1,9)
startColNum = 0;
startRowNum++;
//第1行,绘制表头
ws.addCell(new Label(startColNum, startRowNum, "标题1", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题3", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题3", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题4", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题5", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题6", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题7", wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, "标题8", wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
//添加记录
while (it.hasNext()) {
AccessRecord ar = (AccessRecord) it.next();
ws.addCell(new Label(startColNum, startRowNum, String.valueOf(ar.getId()), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getArOrganization(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getArName(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getPid(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getPname(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getAccessDate() + ar.getAccessTime(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getDepartureDate() + ar.getDepartureTime(), wcf_key));
startColNum++;
ws.addCell(new Label(startColNum, startRowNum, ar.getContent(), wcf_key));
startColNum++;
//将行数加1,列数重置为0
startRowNum++;
startColNum = 0;
}
wb.write(); //生成Excel工作簿
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class AccessRecord {
private String id;
private String arOrganization;
private String arName;
private String pid;
private String pname;
private String accessDate;
private String accessTime;
private String DepartureDate;
private String DepartureTime;
private String content;
public AccessRecord(String id, String arOrganization,
String arName, String pid, String pname,
String accessDate, String accessTime,
String departureDate, String departureTime, String content) {
this.id = id;
this.arOrganization = arOrganization;
this.arName = arName;
this.pid = pid;
this.pname = pname;
this.accessDate = accessDate;
this.accessTime = accessTime;
DepartureDate = DepartureDate;
DepartureTime = DepartureTime;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getArOrganization() {
return arOrganization;
}
public void setArOrganization(String arOrganization) {
this.arOrganization = arOrganization;
}
public String getArName() {
return arName;
}
public void setArName(String arName) {
this.arName = arName;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getAccessDate() {
return accessDate;
}
public void setAccessDate(String accessDate) {
this.accessDate = accessDate;
}
public String getAccessTime() {
return accessTime;
}
public void setAccessTime(String accessTime) {
this.accessTime = accessTime;
}
public String getDepartureDate() {
return DepartureDate;
}
public void setDepartureDate(String departureDate) {
DepartureDate = departureDate;
}
public String getDepartureTime() {
return DepartureTime;
}
public void setDepartureTime(String departureTime) {
DepartureTime = departureTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
<!-- excle表格jar -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
转载自blog.csdn.net/weixin_39513166/article/details/78695302