前言
首先接到这个功能开发的时候,第一个想法就是百度看看API,网上也提供了一些解决方法,但是都不完善,我是在网上资源的基础之上,做的补充,修复了逻辑上的错误。
实现
首先要实现这个功能,我们先要了解excel的api文档,当然,如果快速开发,那你执行以下方法即可
首先下载jxl-2.6.12.jar和jaudiotagger-2.0.3.jar
java代码
package com.happydayin.util;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.*;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
// 稍后优化 2019-12-19 qzx
public class ExcelOperaation {
public static void writeExcel(String path, List<String[]> list, String sheet, String[] title, int pageCount) {
try {
// 创建Excel工作薄
WritableWorkbook wwb = null;
// 新建立一个jxl文件
OutputStream os = new FileOutputStream(path);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
// WritableSheet sheets = wwb.createSheet(sheet, 1);
//获取List集合的size
int totle = list.size();
// 每个工作表格最多存储5条数据(注:excel表格一个工作表可以存储65536条)
int mus = pageCount;
//每页记录数
int count=pageCount;
// 应该创建多少页
int avg = totle / mus;
// 设置初始数据大小
int sum = 0;
// 增加二维码路径转存到excel中有3中情况
if(totle<pageCount){ //1.list长度小于pageCount
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheets = wwb.createSheet("sheet1", 1);
Label label;
for (int i = 0; i < title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
// label = new Label(i, 0, title[i]);
label = new Label(i, 0, title[i], getHeader());
// 设置列宽
sheets.setColumnView(i, 30);
// sheets.setColumnView(4, 100);
// 将定义好的单元格添加到工作表中
sheets.addCell(label);
}
// 设置单元格属性
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
for (int i = 0; i < list.size(); i++) {
String[] arrData = list.get(i);
for (int j = 0; j < arrData.length; j++) {
// 向特定单元格写入数据
// sheets.setColumnView(j, 20);
label = new Label(j, 1 + i, arrData[j], wc);
sheets.addCell(label);
}
}
}else if(totle>pageCount){ //2.list的长度刚好可以整除pageCount
if(totle%pageCount==0){ //整除
for (int s = 1; s <= avg ; s++) {
//创建一个可写入的工作表
WritableSheet sheets = wwb.createSheet("sheet" + s, s);
// 设置列宽
Label label;
for (int i = 0; i < title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
// label = new Label(i, 0, title[i]);
label = new Label(i, 0, title[i], getHeader());
// 设置列宽
sheets.setColumnView(i, 30);
// sheets.setColumnView(4, 100);
// 将定义好的单元格添加到工作表中
sheets.addCell(label);
}
// 设置单元格属性
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
// 填充数据
for (int i = sum; i < mus; i++) {
String[] arrData = list.get(i);
for (int j = 0; j < arrData.length; j++) {
// 向特定单元格写入数据
// sheets.setColumnView(j, 20);
label = new Label(j, 1 + i-sum, arrData[j], wc);
sheets.addCell(label);
}
}
sum=mus;
mus=(s+1)*count;
}
}else if(totle%pageCount>0){ //有余数
int cou = avg+1; //因为有余数,所以sheet数量要加1
for (int s = 1; s <= cou ; s++) {
//创建一个可写入的工作表
WritableSheet sheets = wwb.createSheet("sheet" + s, s);
// 设置列宽
Label label;
for (int i = 0; i < title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
// label = new Label(i, 0, title[i]);
label = new Label(i, 0, title[i], getHeader());
// 设置列宽
sheets.setColumnView(i, 30);
// sheets.setColumnView(4, 100);
// 将定义好的单元格添加到工作表中
sheets.addCell(label);
}
// 设置单元格属性
WritableCellFormat wc = new WritableCellFormat();
// 设置居中
wc.setAlignment(Alignment.CENTRE);
// 设置边框线
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
if(s<cou){ //s小于cou,继续走整除路线
// 填充数据
for (int i = sum; i < mus; i++) {
String[] arrData = list.get(i);
for (int j = 0; j < arrData.length; j++) {
// 向特定单元格写入数据
// sheets.setColumnView(j, 20);
label = new Label(j, 1 + i-sum, arrData[j], wc);
sheets.addCell(label);
}
}
sum=mus;
mus=(s+1)*count;
}else if(s==cou){ //当s和cou相等时
// 取余后的数据
int tot = totle%pageCount+sum;
for (int i = sum; i < tot; i++) {
String[] arrData = list.get(i);
for (int j = 0; j < arrData.length; j++) {
// 向特定单元格写入数据
// sheets.setColumnView(j, 20);
label = new Label(j, 1 + i-sum, arrData[j], wc);
sheets.addCell(label);
}
}
break;
}
}
}
}
// 写入数据
wwb.write();
// 关闭文件
wwb.close();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public static WritableCellFormat getHeader() {
// 定义字体
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD);
try {
// 黑色字体
font.setColour(jxl.format.Colour.BLACK);
}
catch (WriteException e1) {
e1.printStackTrace();
}
WritableCellFormat format = new WritableCellFormat(font);
try {
// 左右居中
format.setAlignment(jxl.format.Alignment.CENTRE);
// 上下居中
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
// 黑色边框
format.setBorder(Border.ALL, BorderLineStyle.THIN, jxl.format.Colour.BLACK);
// 黄色背景
format.setBackground(jxl.format.Colour.YELLOW);
}
catch (WriteException e) {
e.printStackTrace();
}
return format;
}
public static void main(String args[]) {
ExcelOperaation excel = new ExcelOperaation();
// 文件保存路径
String path = "D:/test.xls";
// 准备测试数据 这边传完整的数据,然后excel做分sheet操作
List<String[]> list = new ArrayList<>();
for (int i = 1; i <=2045 ; i++) {
String content[] = { i + "", "测试","测试" };
list.add(content);
}
// sheet页名称
String sheet = "100";
// 列名
String title[] = { "id", "content","思想" };
excel.writeExcel(path, list, sheet, title,1000);
}
}
如上代码即可.在实际开发时,只要将参数转换为动态的即可
上述代码在判断写入sheet时,做了判断,判断逻辑是
1、当总数可以整除每页记录数时
2、当总数不能整除记录数
3、当总数小于记录数