java中做excel表

news/2024/7/21 4:51:21 标签: Excel, Java, VC++, SQL, J#
用到第三方架包:
poi-2.5-final-20040302.jar;
poi-contrib-2.5-final-20040302.jar
poi-scratchpad-2.5-final-20040302.jar


//处理类
public class CountExcel {

/**
* 数据库操作类,自己写吧。。。
*/
private OperData jdbc /*自己写*/;

/**
* 创建excel,返回excel的存放地址
*
* @param title
* 标题
* @param sql
* 查询语句
* @param path
* excel的存放路径(物理地址)
* @param titlename
* 报表的名字
* @return 路径
*/
public String createExcelForAssess(String[] title, String sql, String path,
String titlename) throws Exception {
GregorianCalendar c = new GregorianCalendar();
StringBuffer excelUrl = new StringBuffer();
java.util.Date now = c.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
String strNow = format.format(now);
excelUrl.append(path).append(File.separator).append("te_").append(
strNow).append(".xls");
// excel的表头
Vector vcCol = new Vector();
if (title == null || title.length < 1)
return "";
for (int i = 0; i < title.length; i++) {
vcCol.add(title[i]);
}
int[] align = new int[vcCol.size()];
int[] num = new int[vcCol.size()];
for (int i = 0; i < vcCol.size(); i++) {
align[i] = 2;
num[i] = 0;
}
Vector vc = getqueryrest(sql);
ExcelReport excel = new ExcelReport();
excel.setExcelFile(excelUrl.toString());
excel.setCols(vcCol.size());
excel.createCaption(titlename);
excel.createColumnCaption(vcCol);
excel.createBody(vc, align, num);
excel.createPage(5);
excel.createFile();
return excelUrl.toString();
}

/**
* 查询结果集
*
* @param sql传入查询的sql语句
* @return Vector
* @throws SQLException
*/
public Vector getqueryrest(String sql) throws SQLException {
Vector vc = new Vector();
//数据库查询,返回的list里面存的是数据的pojo对象
//List list = jdbc.getQueryResult(sql);
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
String[] info = (String[]) list.get(i);
for (int j = 0; j < info.length; j++) {
vc.add(info[j]);
}
}
}
return vc;
}
}

//工具类
public class ExcelReport{
/**
* EXCEL文件工作区
*/
private HSSFWorkbook wb;

/**
* EXCEL文件SHEET
*/
private HSSFSheet sheet;

/**
* EXCEL文件存放的目录路径
*/
private String excelFile = "";

/**
* 记录当前行数
*/
private int rownum = 0;

/**
* 记录总共列数
*/
private int cols = 0;

/**
* 记录每列的宽度
*/
private int[] length;

/**
* 记录是否已经设定字段数
*/
private boolean flag = false;

/**
* 设置EXCEL文件存放的目录路径,在生成标题,列头前设定
*/
public void setExcelFile(String excelFile){
this.excelFile = excelFile;
}

/**
* 设置EXCEL表的列数,在生成标题,列头前设定
*/
public void setCols(int cols){
this.cols = cols;
if(flag){
if(length.length < cols){
length = getLength(length);
}
}else{
length = new int[cols];
}
flag = true;
}

/**
* 第二次设定字段数,保存第一张表格每列的长度
*/
private int[] getLength(int[] arr){
int[] temp = new int[cols];
for(int i=0;i<arr.length;i++){
temp[i] = arr[i];
}
return temp;
}

/**
* 初始化EXCEL报表类
*/
public ExcelReport(){
wb = new HSSFWorkbook();
sheet = wb.createSheet("new sheet");
}

/**
* 生成标题
* @param caption 标题头
*/
public void createCaption(String caption){
//生成标题行
HSSFRow row = sheet.createRow((short)rownum);
//生成标题单元格
HSSFCell cell = row.createCell((short)0);
//生成标题单元格样式
HSSFCellStyle style = wb.createCellStyle();

//设定标题字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)14);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);

//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式
cell.setCellValue(caption);
cell.setCellStyle(style);

for(int i=1;i<cols;i++){
cell = row.createCell((short)i);
cell.setCellStyle(style);
cell.setCellValue("");
}

//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));

//当前行自增
rownum++;
}

/**
* 生成列头
* @param vc 列头内容
*/
public void createColumnCaption(Vector vc){
//生成列头行
HSSFRow row = sheet.createRow((short)rownum);

//生成了列头格式
HSSFCellStyle style = wb.createCellStyle();

//设定列头字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);

//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

//生成列头单元格
HSSFCell cell;
for(int i=0;i<vc.size();i++){
//生成标题单元格
cell = row.createCell((short)i);
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
if(vc.get(i) != null){
cell.setCellValue(vc.get(i).toString());
//记录列头的长度
if(vc.get(i).toString().getBytes().length > length[i]){
length[i] = vc.get(i).toString().getBytes().length;
}
}else{
cell.setCellValue("");
}
cell.setCellStyle(style);
}
rownum++;
}

/**
* 生成合并的列头
* @param vc 列头
* @param colnum 每列要合并的列数,并且所有要合并的列数之和等于总表格列数
*/
public void mergeColumnCaption(Vector vc,int[] colnum){
//生成标题行
HSSFRow row = sheet.createRow((short)rownum);

//生成标题单元格样式
HSSFCellStyle style = wb.createCellStyle();

//设定标题字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
style.setFont(font);

//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

int pos = 0;
HSSFCell cell;
for(int i=0;i<vc.size();i++){
pos = pos + colnum[i];
cell = row.createCell((short)(pos-colnum[i]));
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设定单元格文字合样式
cell.setCellStyle(style);
if(vc.get(i) == null){
cell.setCellValue("");
}else{
cell.setCellValue(vc.get(i).toString());
}
for(int j=1;j<colnum[i];j++){
cell = row.createCell((short)(pos-colnum[i]+j));
cell.setCellStyle(style);
cell.setCellValue("");
}
//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)(pos-colnum[i]),rownum,(short)(pos-1)));
}
//当前行自增
rownum++;
}

/**
* 合并行
* @param startrow 起始行
* @param endrow 终止行
* @param column 列数
*/
public void mergeRowCaption(int startrow,int endrow,int column){
sheet.addMergedRegion(new Region(startrow,(short)column,endrow,(short)column));
}

/**
* 生成表格主体
* @param vc 表格内容
* @param align 每列的对齐方式,1为左,2为中,3为右,数组长度要等于表格列数
* @param num 每列的数据类型,0为字符串,1为数字
*/
public void createBody(Vector vc,int[] align,int[] num){
int rows = vc.size() / cols;
//设定表格字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFRow row;
HSSFCell cell;

//设定数据格式
HSSFDataFormat df = wb.createDataFormat();

//生成了左对齐表格格式
HSSFCellStyle styleLeft = wb.createCellStyle();
styleLeft.setFont(font);
styleLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleLeft.setBottomBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleLeft.setLeftBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleLeft.setRightBorderColor(HSSFColor.BLACK.index);
styleLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleLeft.setTopBorderColor(HSSFColor.BLACK.index);
styleLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
styleLeft.setDataFormat(df.getFormat("##################.00"));

//生成居中对齐表格格式
HSSFCellStyle styleCenter = wb.createCellStyle();
styleCenter.setFont(font);
styleCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleCenter.setBottomBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleCenter.setLeftBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleCenter.setRightBorderColor(HSSFColor.BLACK.index);
styleCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleCenter.setTopBorderColor(HSSFColor.BLACK.index);
styleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleCenter.setDataFormat(df.getFormat("##################.00"));

//生成右对齐表格格式
HSSFCellStyle styleRight = wb.createCellStyle();
styleRight.setFont(font);
styleRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleRight.setBottomBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleRight.setLeftBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleRight.setRightBorderColor(HSSFColor.BLACK.index);
styleRight.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleRight.setTopBorderColor(HSSFColor.BLACK.index);
styleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
styleRight.setDataFormat(df.getFormat("##################.00"));

for (int i = 0;i < rows; i++){
// 创建新行
row = sheet.createRow((short)(rownum));
for (int j = 0;j < cols; j++){
// 创建一个单元格
cell = row.createCell((short)j);
//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置cell字符类型的值
if(vc.get(i*cols+j) == null){
cell.setCellValue("");
}else{
if(num[j] == 0){
cell.setCellValue(vc.get(i*cols+j).toString());
}else{
cell.setCellValue(Double.parseDouble(vc.get(i*cols+j).toString()));
}
//记录每列的长度
if(vc.get(i*cols+j).toString().getBytes().length > length[j]){
length[j] = vc.get(i*cols+j).toString().getBytes().length;
}
}
//设定对齐方式
if(align[j] == 1){
cell.setCellStyle(styleLeft);
}
if(align[j] == 2){
cell.setCellStyle(styleCenter);
}
if(align[j] == 3){
cell.setCellStyle(styleRight);
}
}
rownum++;
}
}

/**
* 生成统计结果行
* @param stat 统计结果
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createStat(String stat,int align){
//生成统计结果行
HSSFRow row = sheet.createRow((short)rownum);
//生成统计结果格
HSSFCell cell = row.createCell((short)0);
//生成统计结果单元格样式
HSSFCellStyle style = wb.createCellStyle();

//设定统计结果字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLACK.index);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);

//设定标题框格式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);

//设定对齐方式

if(align == 1){
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
}
if(align == 2){
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
}
if(align == 3){
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
}

//设置cell编码解决中文高位字节截断
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

//设定单元格文字合样式
cell.setCellValue(stat);
cell.setCellStyle(style);

for(int i=1;i<cols;i++){
cell = row.createCell((short)i);
cell.setCellStyle(style);
cell.setCellValue("");
}

//设定合并的单元格
sheet.addMergedRegion(new Region(rownum,(short)0,rownum,(short)(cols-1)));

//当前行自增
rownum++;
}

/**
* 设置页眉
* @param header 页眉内容
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createHeader(String header,int align){
HSSFHeader head = sheet.getHeader();
if(align == 1){
head.setLeft(header);
}
if(align == 2){
head.setCenter(header);
}
if(align == 3){
head.setRight(header);
}
}

/**
* 设置页脚
* @param footer 页脚内容
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createFooter(String footer,int align){
HSSFFooter foot = sheet.getFooter();
if(align == 1){
foot.setLeft(footer);
}
if(align == 2){
foot.setCenter(footer);
}
if(align == 3){
foot.setRight(footer);
}
}

/**
* 设定页脚的页面值
* @param align 对齐方式,1为左,2为中,3为右
*/
public void createPage(int align){
HSSFFooter foot = sheet.getFooter();
if(align == 1){
foot.setLeft("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
if(align == 2){
foot.setCenter("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
if(align == 3){
foot.setRight("Page:" + HSSFFooter.page() + "/" + HSSFFooter.numPages());
}
}

/**
* 生成EXCEL文件
*/
public void createFile() throws Exception{
for(int i=0;i<length.length;i++){
sheet.setColumnWidth((short)i,(short)(length[i]*2*200));
}
FileOutputStream fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
fileOut.close();
}

http://www.niftyadmin.cn/n/595263.html

相关文章

python filelock_Pipfile.lock文件已经过时了。

试图将python脚本推送到heroku&#xff0c;它抱怨说pipfile.lock文件已经过时了。我使用pipenv来管理依赖关系&#xff0c;并且我尝试运行pipenv lock来更新锁文件&#xff0c;但是没有成功。在Counting objects: 11, done.Delta compression using up to 4 threads.Compressin…

如何通过SecureCRT转发功能实现外网访问内网服务

公司有台开发服务器有外网地址&#xff0c;开放了ssh服务&#xff0c;上面装有mysql服务&#xff0c;MySQL服务端口3306是没有对外网开放的。所以如果在家要连接这台服务器的mysql数据库得另外想其他办法。好在SecureCRT提供了端口转发的功能&#xff0c;能够通过SecureCRT将内…

Weekly Contest 64题解

Largest Number Greater Than Twice of Others 题意 判断一个数组最大的元素是否是其他元素的至少2倍 思路 循环判断一下就好了 代码 class Solution { public:int dominantIndex(vector<int>& nums) {if (nums.empty()) {return -1;}int n nums.size();int max_idx…

linux下如何解决jvm执行取得的时间和系统时间不一致的问题

今天在和对端系统调试接口的时候发现一个怪问题。用本地环境调试调用接口没有问题&#xff0c;发到服务器上问题就来了参数解析不了。确认了版本没有问题以后&#xff0c;一步步分析。这接口对端要求我们参数通过AES加密传过去&#xff0c;秘钥是一串字符加年月日的时间戳。怀疑…

js form action动态修改方法

用javaScript动态修改html组件form的action属性&#xff0c;可以在提交时再决定处理表单的页面。Js代码 < type"application/x-shockwave-flash" width"14" height"15" src"http://wjt276.iteye.com/javascripts/syntaxhighlighter/clip…

cad工具箱详细讲解_cad学院派工具箱(cad绘图教程配解析)V20160804 最新版

cad学院派工具箱(cad绘图教程配解析)是一款功能强大的cad辅助软件,用户们通过这款晶锐可以更加轻松的制图绘图,并且使用方法和安装教程都为用户们附上,感兴趣的朋友们快来下载吧!安装说明:CAD调用外部程序的方法,常用的CAD外部程序有LISP语言编写的LSP文件,VLX文件等,以学院派C…

SOA和ESB的区别

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/csdnblog01.html" frameborder"0" width"728" scrolling"no" height"90"></iframe>本期的BIJ…

java转JSON串的几种方式

昨天在与对端系统调接口的时候&#xff0c;对端系统对我们传过去的json串老是处理不了&#xff0c;后来查原因是应为我们传过去的json串里有json对象数组&#xff0c;因为我们的json串存在表里的&#xff0c;取出来是作为json字符串放到json数组里的&#xff0c;所以带了双引号…