Excel 写入(jxl)

package com.suning.crawler.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.log4j.Logger;

/**
* 将数据导出Excel
*
* @author xxx
*/
public class ExcelWrite
{
// 日志记录
private Logger logger = Logger.getLogger(ExcelWrite.class);

/**
* 标题行
*/
private List<String> title;

/**
* 构造函数
*
* @param titles 标题,用","分隔
*/
public ExcelWrite(String titles)
{
title = new ArrayList<String>();
// 标题
if (null != titles)
{
for (String titleName : titles.split(","))
{
title.add(titleName);

}
}
}

/**
* 构造函数
*
* @param title 标题
*/
public ExcelWrite(List<String> title)
{
this.title = title;
}

/**
* 将数据信息写入Excel文件
*
* @param datas 数据信息
* @param fileName 文件名称
* @param filePath 导出文件路径,默认路径是output
*/
public void WriteData(List<List<String>> datas, String fileName, String filePath)
{
// 目录
File dir = new File(filePath);
if (!dir.exists())
{
dir.mkdirs();
}

// 创建文件
File file = new File(filePath + fileName);
WriteData(datas, file);
}

/**
* 将数据信息写入Excel文件
*
* @param datas 数据信息
* @param file 文件
*/
public void WriteData(List<List<String>> datas, File file)
{
String sheetName = file.getName();
sheetName = sheetName.substring(0, sheetName.lastIndexOf("."));

WritableWorkbook wb = null;
try
{
// 打开文件
wb = Workbook.createWorkbook(file);
WritableSheet sheet = wb.createSheet(sheetName, 0);

// 创建标题
Label[] titleLabels = getLabel(0, title);
for (Label label : titleLabels)
{
sheet.addCell(label);
}

// 把内容写入文件中
int index = 0;
if (null != title && !title.isEmpty())
{
index = 1;
}
for (List<String> record : datas)
{
Label[] productLabels = getLabel(index, record);
for (Label label : productLabels)
{
sheet.addCell(label);
}
++index;
}
wb.write();
}
catch (RowsExceededException e)
{
logger.error("Exception is happened when jxl write rows is exceeded.", e);
}
catch (WriteException e)
{
logger.error("Write the file exception", e);
}
catch (IOException e)
{
logger.error("write the file fails", e);
}
catch (Exception e)
{
logger.error("other exception", e);
}
finally
{
// 关闭写
if (null != wb)
{
try
{
wb.close();
}
catch (WriteException e)
{
logger.error("write the file fails", e);
}
catch (IOException e)
{
logger.error("write the file fails", e);
}
}
}

}

/**
* 将商品信息封装成Excel列
*
* @param row Excel行
* @param datas 数据信息
* @return 数据信息对应的列
*/
private Label[] getLabel(int row, List<String> datas)
{
Label[] label = new Label[datas.size()];
for (int i = 0; i < datas.size(); i++)
{
label[i] = new Label(i, row, datas.get(i));
}

return label;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值