jxl操作excel

    /**
* 分隔符
*/
private final static String SEPARATOR = "|"; /**
* 由List导出至指定的Sheet,带total行(最后一行)
* @param wb 模板的workbook
* @param sheetNum 第几个表单
* @param targetFilePath 生成文件夹路径
* @param l 内容list集合,以|分割的对象string集合
* @param headInfoRows 头信息的行数
* @param columnsLength 列数
* @param remarkRowNumber 备注所在行
* @param remark 备注
* @return
* @throws WriteException
* @throws IOException
* int
*/
public static int exportExcelFromList(jxl.Workbook wb, int sheetNum,
String targetFilePath, List<String> l, int headInfoRows,
int columnsLength,int remarkRowNumber,String remark) throws WriteException, IOException {
// 创建可写入的Excel工作薄对象
WritableWorkbook wwb = null;
int writeCount = 0; // 单元格样式
// WritableFont bold = new
// WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat normalFormat = new WritableCellFormat(
NumberFormats.TEXT);
normalFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
jxl.format.Colour.BLACK); //设置字体;
WritableFont font = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat normalFormat_total = new WritableCellFormat(
font);
normalFormat_total.setBorder(Border.ALL, BorderLineStyle.THIN,
jxl.format.Colour.BLACK); try { // 创建可写入的Excel工作薄对象
wwb = jxl.Workbook.createWorkbook(new File(targetFilePath), wb);
WritableSheet ws = wwb.getSheet(0); Label cellRemark = new Label(0, remarkRowNumber, remark,
normalFormat);
ws.addCell(cellRemark); int row = l.size();
int columns = columnsLength;
String[] ary = new String[120]; for (int i = 0; i < row; i++) {
ary = l.get(i).split("\\" + SEPARATOR);
for (int j = 0; j < columns; j++) { if(i==row-1)
{
Label cell = new Label(j, i + headInfoRows, ary[j],
normalFormat_total);
ws.addCell(cell);
}else
{
Label cell = new Label(j, i + headInfoRows, ary[j],
normalFormat);
ws.addCell(cell);
}
}
writeCount++;
}
wwb.write();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (wwb != null) {
wwb.close();
} } return writeCount; } /**
* 导出不需要合计行
* @param wb
* @param sheetNum
* @param targetFilePath
* @param l
* @param headInfoRows
* @param columnsLength
* @param remarkRowNumber
* @param remark
* @return
* @throws WriteException
* @throws IOException
*/
public static int exportExcelFromListNoTotal(jxl.Workbook wb, int sheetNum,
String targetFilePath, List<String> l, int headInfoRows,
int columnsLength,int remarkRowNumber,String remark) throws WriteException, IOException {
// 创建可写入的Excel工作薄对象
WritableWorkbook wwb = null;
int writeCount = 0; // 单元格样式
// WritableFont bold = new
// WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD);//设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
WritableCellFormat normalFormat = new WritableCellFormat(
NumberFormats.TEXT);
normalFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
jxl.format.Colour.BLACK); //设置字体;
WritableFont font = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat normalFormat_total = new WritableCellFormat(
font);
normalFormat_total.setBorder(Border.ALL, BorderLineStyle.THIN,
jxl.format.Colour.BLACK); try { // 创建可写入的Excel工作薄对象
wwb = jxl.Workbook.createWorkbook(new File(targetFilePath), wb);
WritableSheet ws = wwb.getSheet(0); Label cellRemark = new Label(0, remarkRowNumber, remark,
normalFormat);
ws.addCell(cellRemark); int row = l.size();
int columns = columnsLength;
String[] ary = new String[120]; for (int i = 0; i < row; i++) {
ary = l.get(i).split("\\" + SEPARATOR);
for (int j = 0; j < columns; j++) { Label cell = new Label(j, i + headInfoRows, ary[j],
normalFormat);
ws.addCell(cell);
}
writeCount++;
}
wwb.write();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (wwb != null) {
wwb.close();
} } return writeCount; }
exportList为List<String>,生成方式为遍历每个对象并将所有属性以|串起来
        List<DetectorHistory> dfList = service.getList();   //获取对象集合

        List<String> exportList = new ArrayList<String>();
StringBuffer sbList = new StringBuffer();
if (dfList!=null&&dfList.size()>0) {
for (DetectorHistory ele:dfList) {
sbList.delete(0, sbList.length());
          //加入|
//加入属性
...
exportList.add(sbList.toString());
}
}
controller层
        //模板所在文件夹路径
String tempPath = req.getSession().getServletContext()
.getRealPath(CommonValue.FileTemplatePath);
//生成文件所在文件夹路径
String exportFilePath = req.getSession().getServletContext()
.getRealPath(CommonValue.ExportFilePath);
//导出文件名
String exportFileName = "";
//模板文件名,事先生成好以及头文件情况
String targetFileName = "ReportTmp_detectorHistory.xls"; //生成的行数
int operatorCount = 0; JSONObject jsonObject = new JSONObject();
try {
// 文件导出
if (exportList.size() > 0) {
//生成文件名
exportFileName = "detectorHistory" + CommonTool.getNowDateStr2()
+ "." + targetFileName.split("\\.")[1];
//利用模板生成Workbook
Workbook rw = jxl.Workbook.getWorkbook(new File(tempPath
+ File.separator + targetFileName)); // 写入备注文件
String remarkInfo = "统计时间:" + CommonTool.getNowDateStr2() + " 金额单位:元"; //rw为模板workbook,0为sheetnum,其次为导出文件路径,exportList为|分割属性的string对象集合,4为头的行数,14为列数,1为备注所在行(从0开始),remarkinfo为备注所在行的信息
operatorCount = ExcelHelper_ChargeSituation.exportExcelFromListNoTotal(rw,
0, exportFilePath + File.separator + exportFileName,
exportList, 4, 14, 1, remarkInfo); } jsonObject.put("operatorCount", operatorCount); //返回操作条数
jsonObject.put("exportFilePath", CommonValue.ExportFilePath
+ File.separator + exportFileName); //返回生成的文件路径 if(jsonObject.get("operatorCount")!=null&&Integer.valueOf(jsonObject.get("operatorCount").toString())<=0)
{//当生成内容条数为0时
jsonObject.put("rtnCode", "404");
}else
{
jsonObject.put("rtnCode", "0");
}
} catch (BiffException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}finally{ } res.resetBuffer();
res.setContentType("text/html;charset=UTF-8");
res.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
res.getOutputStream().flush();
return null;

jxl将list导入到Excel中供下载的更多相关文章

  1. Excel批量导入商品,遇到导入失败记录到另一个Excel中供下载查看

    /// <summary> /// EXCEL批量导入 /// </summary> /// <param name="filePath">文件 ...

  2. 如何使用免费控件将Word表格中的数据导入到Excel中

    我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...

  3. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  4. phpexcel的写操作将数据库中的数据导入到excel中

    这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...

  5. 从输出日志中提取接口的入参和返回做为用例导入到excel中

    1  背景 接口用例已经在项目中的yml文件中编写,但是yml文件不能做为交付文档用,本文对工作中从接口输出日志中提取用例信息,并导入到excel文件中做了总些 2  工具 idea,notepad+ ...

  6. [转] JAVA中读取网络中的图片资源导入到EXCEL中

    需求 导出人员的信息并且加上人员的照片至EXCEL中 完整的代码 //创建一个表格 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...

  7. 将网页上指定的表单的数据导入到excel中

    很多时候,我们想要将网页上显示的信息,导入到Excel中,但是很多时候无法下手.可是,这个时候,下面这个例子会帮你大忙了. 将html表单指定内容导出到EXCEL中. <!DOCTYPE HTM ...

  8. 小技巧之“将Text文件中的数据导入到Excel中,这里空格为分割符为例”

    1.使用场景 将数据以文本导出后,想录入到Excel中,的简便方案, 起因:对于Excel的导出,Text导出明显会更方便些 2.将Text文件中的数据导入到Excel中,这里空格为分割符为例的步骤 ...

  9. 将页面中指定表格的数据导入到Excel中

    function AutoExcel(){   var oXL = new ActiveXObject("Excel.Application"); //创建应该对象   var o ...

随机推荐

  1. spriing boot 启动报错:Cannot determine embedded database driver class for database type NONE

    最近在学习使用spring boot.使用maven创建好工程,只引用需要用到的spring boot相关的jar包,除此之外没有任何的配置. 写了一个最简单的例子,如下所示: package com ...

  2. Pandas使用to_csv保存中文数据用Excel打开是乱码

    关于这个问题还是困扰了很久,我生成了一些样本数据,打算保存到csv文件,之后用pandas的命令: # data是DataFrame的格式 data.to_csv('./data/myfile.csv ...

  3. AttributeError: 'module' object has no attribute 'face'

    报错 raceback (most recent call last): File "D:/work/python/face_ai/predict.py", line 41, in ...

  4. Go Configure Support hot reloading.

    Go Configure – Josh Betz https://josh.blog/2017/04/go-configure Go Configure APRIL 27, 2017 # DEVELO ...

  5. wireshark, loopback

    swapondd if=/dev/zero of=/data/mnt/swap bs=1024 count=8024000 sudo apt-get install wireshark sudo gr ...

  6. python re.sub 括号匹配替换匹配到的内容后接数字

    如果代码为: text = re.sub(r'(?<=[{])([a-z]+)6(?=[}])', r'\13', text) 上面代码会报错,因为没有组合13,所以不能获得组合13的内容. 但 ...

  7. python的pip的配置文件路径在哪里?如何修改pypi源?

    官方文档: https://pip.pypa.io/en/stable/user_guide/#configuration 举个例子: Windows用户想要更改pypi源,可以在%APPDATA%目 ...

  8. Django 的 Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使用: 1.定义规则: from ...

  9. Python开发【模块】:matplotlib 绘制折线图

    matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib # 如 ...

  10. nodejs中Async详解之一:流程控制

    为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程控制:简化十种 ...