通过excel模板生成xls模板文件

有种特殊的情况,客户需要的excell表头搞得很炫,要人工去写代码实现比较费事。

可以先存一个模板,然后再在这个模板的基础上写数据。


def wait_shipments(warehouse_id = 1)
search_date = Time.now
file = Spreadsheet.open "#{Rails.root}/public/xls/wait_shipments.xls"
list = file.worksheet 0
Shipment.where(status:0,warehouse_id:warehouse_id).map{ |shipment| shipment.order_details }.flatten.each_with_index do |order_detail,index|
list[index+3,0] = order_detail.customer_order&.customer_order_no
list[index+3,1] = "上海网"
list[index+3,2] = "张先生"
list[index+3,3] = "xxxx"
list[index+3,4] = "重庆市江北区盘溪2支路"
list[index+3,5] = order_detail.customer_order&.member&.company_name
list[index+3,6] = order_detail.address&.ship_contact
list[index+3,8] = order_detail.address&.ship_contacttel
list[index+3,9] = order_detail.address&.ship_address
list[index+3,10] = "寄付现结"
list[index+3,12] = "原料"
list[index+3,13] = 1
list[index+3,14] = 1
list[index+3,17] = "顺丰隔日"
end
xls_report = StringIO.new
file.write xls_report
send_data xls_report.string, :type => 'text/xls', :filename => "wait_shipments_#{search_date.to_s(:db)}.xls"
end
//region 数据源 List resultList = new ArrayList(); Model model = new Model(); model.setCode("01"); model.setName("测试1"); resultList.add(model); model = new Model(); model.setCode("02"); model.setName("测试2"); resultList.add(model); //endregion //获得模版 File directory = new File("src/main/resources/templates"); String templatePath = directory.getCanonicalPath(); String templateFile = templatePath + "\\template.xls"; //导出文件名 SimpleDateFormat simpl = new SimpleDateFormat("yyyyMMddHHmmss"); String currntTime = simpl.format(new Date()); String fileName = currntTime + "报表.xls"; //将结果放入 Map beans = new HashMap(); List values = new ArrayList(); values.add(resultList); beans.put("values", values); beans.put("title", "测试title"); //文件名称统一编码格式 fileName = URLEncoder.encode(fileName, "utf-8"); //生成的导出文件 File destFile = File.createTempFile(fileName, ".xls"); //transformer转到Excel XLSTransformer transformer = new XLSTransformer(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //将数据添加到模版中生成新的文件 transformer.transformXLS(templateFile, beans, destFile.getAbsolutePath()); //将文件输入 InputStream inputStream = new FileInputStream(destFile); //设置response参数,可以打开下载页面 response.reset(); //设置响应文本格式 response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1")); //将文件输出到页面 ServletOutputStream out = response.getOutputStream(); bis = new BufferedInputStream(inputStream); bos = new BufferedOutputStream(out)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值