excel转网页服务器,jxl 生成excel,从服务器导出excel到网页

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public static void initExcel(String fileName, String[] colName,

int[] widthArr) {

JXLUtil.index = 0;// 设置为初始值。不然static的index会一直递增

JXLUtil.row = 2;

format();// 先设置格式

WritableWorkbook workbook = null;

try {

// WorkbookSettings setEncode = new WorkbookSettings(); // 设置读文件编码

// setEncode.setEncoding(UTF8_ENCODING);

File file = new File(fileName);

workbook = Workbook.createWorkbook(file);

LOG.debug("工作环境创建成功");

WritableSheet sheet = workbook.createSheet("Sheet 1", 0);// 建立sheet

sheet.mergeCells(0, 0, colName.length - 1, 0);

sheet.addCell((WritableCell) new Label(0, 0, fileName,

arial14format));// 表头设置完成

for (int i = 0; i < widthArr.length; i++) {

sheet.setColumnView(i, widthArr[i]);// 设置col 宽度

}

int row = 1;

int col = 0;

for (col = 0; col < colName.length; col++) {

sheet.addCell(new Label(col, row, colName[col], arial10format));// 写入

// col名称

}

workbook.write();// 写入数据

LOG.debug("init ok!");

} catch (RowsExceededException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (workbook != null) {

try {

workbook.close();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

/**

* 将 数据写入到excel中

*

* @param os

* 创建Excel的输出流

* @param objList

* 要插入的数据

* @param fieldArr

* 字段名称

* @param fileName

* excel表头名称

* @param colName

* excel列明

* @param widthArr

* excel单元格宽度

*/

public static void dataToExcel(ByteArrayOutputStream os,

List objList, String[] fieldArr, String fileName,

String[] colName, int[] widthArr) {

format();// 先设置格式

WritableWorkbook workbook = null;

try {

// WorkbookSettings setEncode = new WorkbookSettings(); // 设置读文件编码

// setEncode.setEncoding(UTF8_ENCODING);

// File file = new File(fileName);

workbook = Workbook.createWorkbook(os);

LOG.debug("工作环境创建成功");

WritableSheet sheet = workbook.createSheet("Sheet 1", 0);// 建立sheet

sheet.mergeCells(0, 0, colName.length - 1, 0);

sheet.addCell((WritableCell) new Label(0, 0, fileName,

arial14format));// 表头设置完成

for (int i = 0; i < widthArr.length; i++) {

sheet.setColumnView(i, widthArr[i]);// 设置col 宽度

}

int row = 1;

int col = 0;

for (col = 0; col < colName.length; col++) {// 写入列明

sheet.addCell(new Label(col, row, colName[col], arial10format));// 写入

// col名称

}

for (Object tmp : objList) {// 写入数据

row++;

col = 0;

index++;

String serialNumberStr = String.valueOf(index);

sheet.addCell(new Label(col, row, serialNumberStr,

arial12format));// 第一列用来写序号

col++;

/**

* 通过反射取值,并且写入到excel中

*/

for (int i = 0; i < fieldArr.length; i++) {

String fieldName = fieldArr[i];

Object value = GetValueByRef.getValueByRef(tmp, fieldName);

String str = null;

if (value == null) {

str = "";

} else {

str = String.valueOf(value);

}

sheet.addCell(new Label(col, row, str, arial12format));

col++;

}

}

workbook.write();// 写入数据

LOG.debug("aaaaaaa write finished!");

} catch (RowsExceededException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (workbook != null) {

try {

workbook.close();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (os != null) {

try {

os.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static void writeObjListToExcel(List objList,

String fileName, String[] fieldArr) {

if (objList != null && objList.size() > 0) {

WritableWorkbook writebook = null;

InputStream in = null;

try {

/**

* 读取原来写入的文件

*/

// WorkbookSettings setEncode = new WorkbookSettings();

// //设置读文件编码

// setEncode.setEncoding(UTF8_ENCODING);

in = new FileInputStream(new File(fileName));

Workbook workbook = Workbook.getWorkbook(in);

writebook = Workbook.createWorkbook(new File(fileName),

workbook);

WritableSheet sheet = writebook.getSheet(0);

/**

* 写入数据

*/

// {"序号","体验卡号","手机号码","用户iTV帐号","地区","使用时间","体验到期时间","体验期是否订购"};

for (Object tmp : objList) {

Class classType = tmp.getClass();

int col = 0;

index++;

String serialNumberStr = String.valueOf(index);

sheet.addCell(new Label(col, row, serialNumberStr,

arial12format));// 第一列用来写序号

col++;

/**

* 通过反射取值,并且写入到excel中

*/

for (int i = 0; i < fieldArr.length; i++) {

String fieldName = fieldArr[i];

Object value = GetValueByRef.getValueByRef(tmp,

fieldName);

String str = null;

if (value == null) {

str = "";

} else {

str = String.valueOf(value);

}

sheet.addCell(new Label(col, row, str, arial12format));

col++;

}

row++;

}

writebook.write();

LOG.debug("报表写入成功");

} catch (BiffException e) {

e.printStackTrace();

} catch (WriteException e) {

LOG.debug("报表写入失败");

e.printStackTrace();

} catch (IOException e) {

LOG.debug("io 异常");

} finally {

if (writebook != null) {

try {

writebook.close();

} catch (WriteException e) {

LOG.error("excel关闭异常");

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}if(in != null){

try {

in.close();

} catch (IOException e) {

LOG.debug(" 文件流关闭异常");

e.printStackTrace();

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值