生成一个 有样式的 excel 文件
来源:互联网 发布:家具市场数据分析 编辑:程序博客网 时间:2024/05/18 01:51
还是三部 , 第二部中 加入了 设置头的样式,* 设置标题样式,设置其他单元格样式
1.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <!-- 花花的 生成excel 格式 --> <package name="hhexcel" extends="struts-default" namespace="/hhexcel"> <action name="JexcelAction" class="com.hangyuan.excel.JexcelAction"> <result name="excel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">is</param> <param name="contentDisposition">filename="export.xls"</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
2.action
package com.hangyuan.excel;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import jxl.Workbook;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.Colour;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import jxl.write.biff.RowsExceededException;import com.opensymphony.xwork2.ActionSupport;public class JexcelAction extends ActionSupport{/** * */private static final long serialVersionUID = 1L;/*** 写excel文件*http://blog.csdn.net/laliocat/article/details/50580020*http://127.0.0.1:8080/ccc/hhexcel/JexcelAction.action*//* * 分析:1 创建 WritableWorkbook 对象 * 2.创建输出流对象 * 3.创建表sheet * */InputStream is;@Overridepublic String execute() throws Exception {System.out.println("--");method();return "excel";}public void method() throws IOException, WriteException{//1 创建 WritableWorkbook 对象//2.创建输出流对象ByteArrayOutputStream os = new ByteArrayOutputStream();WritableWorkbook wb = Workbook.createWorkbook(os);//创建Excel工作表 WritableSheet ws = wb.createSheet("通讯录", 0);//创建sheetws.mergeCells(0, 0, 2, 1);//合并单元格(左列,左行,右列,右行)从第1行第1列到第2行第3列Label header = new Label(0, 0, "通讯录(191026班)", getHeader()); ws.addCell(header);//写入头Label l = new Label(0, 2, "姓名", getTitle());//第3行ws.addCell(l);l = new Label(1, 2, "电话", getTitle());ws.addCell(l);l = new Label(2, 2, "地址", getTitle());ws.addCell(l);l = new Label(0, 3, "小祝", getNormolCell());//第4行ws.addCell(l);l = new Label(1, 3, "1314***0974", getNormolCell());ws.addCell(l);l = new Label(2, 3, "武汉武昌", getNormolCell());ws.addCell(l);l = new Label(0, 4, "小施", getNormolCell());//第5行ws.addCell(l);l = new Label(1, 4, "1347***5057", getNormolCell());ws.addCell(l);l = new Label(2, 4, "武汉武昌", getNormolCell());ws.addCell(l);ws.setColumnView(0,20);//设置列宽ws.setColumnView(1,20);ws.setColumnView(2,40);ws.setRowView(0,400);//设置行高ws.setRowView(1,400);ws.setRowView(2,500);ws.setRowView(3,500);ws.setRowView(4,500);//输出流wb.write();//关闭流wb.close();//outStream.close();System.out.println("写入成功!\n"); is = new ByteArrayInputStream(os.toByteArray());}/*** 设置头的样式* @return */public WritableCellFormat getHeader(){WritableFont font = new WritableFont(WritableFont.TIMES, 24 ,WritableFont.BOLD);//定义字体try {font.setColour(Colour.BLUE);//蓝色字体} catch (WriteException e1) {// TODO 自动生成 catch 块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,Colour.BLACK);//黑色边框format.setBackground(Colour.YELLOW);//黄色背景} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}/*** 设置标题样式* @return*/public WritableCellFormat getTitle(){WritableFont font = new WritableFont(WritableFont.TIMES, 14);try {font.setColour(Colour.BLUE);//蓝色字体} catch (WriteException e1) {// TODO 自动生成 catch 块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,Colour.BLACK);} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}/*** 设置其他单元格样式* @return*/public WritableCellFormat getNormolCell(){//12号字体,上下左右居中,带黑色边框WritableFont font = new WritableFont(WritableFont.TIMES, 12);WritableCellFormat format = new WritableCellFormat(font);try {format.setAlignment(jxl.format.Alignment.CENTRE);format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);} catch (WriteException e) {// TODO 自动生成 catch 块e.printStackTrace();}return format;}public InputStream getIs() {return is;}public void setIs(InputStream is) {this.is = is;}}
3. 访问
*http://127.0.0.1:8080/ccc/hhexcel/JexcelAction.action
阅读全文
0 0
- 生成一个 有样式的 excel 文件
- JSP生成复杂样式的Excel文件
- 通过jxl来生成,有单元格样式的excel
- JAVA生成一个excel文件
- 一个高效的导出Excel方法,并且已解决导出会有样式文件夹的问题
- 读取指定目录下的所有文件生成一个excel
- poi生成Excel文件【带样式和格式】
- 使用JXL来生成一个Excel文件
- 用php生成Excel文档 不适合做为数据源,我是为一个支付平台做Excel结果说不是标准的excel。但是样式很整齐
- poi生成excel,在生成下拉框的时候,数据比较大,报错,有没有有解决方案的,求发一个,谢谢.下面有具体内容
- POI生成Excel常见的几种样式的设置
- poi生成excel 样式控制
- JAVA处理Excel文件---生成简单的Excel文件
- JAVA处理Excel文件---生成简单的Excel文件
- 生成简单的Excel文件的示例
- 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下:
- 在QT中,有一个工具qmake可以生成一个makefile文件,它是由.pro文件生成而来的,.pro文件的写法如下
- C#生成Excel文件的方法
- yarn ResourceManager
- 为您解惑:HTML5中使用MathML数学公式的简单讲解..........
- Sass/SCSS(关系,安装,webstorm配置编译,使用,语法)
- 20171102
- hidden symbol `pthread_atfork' in /usr/lib/x86_64-linux-gnu/libpthread_nonshared.a(pthread_atfork.oS
- 生成一个 有样式的 excel 文件
- statsmodels 安装方法
- android支付宝调用
- vue
- MySql-聚集索引-表为什么必须有主键
- 采集适配
- 第十五篇 深度揭秘elasticsearch的搜索技术
- Jaxb2 实现JavaBean与xml互转
- virtualbox虚拟机centos7 连接ssh问题