jxl导出excel --- 居中 自动换行 边框 样式

public void writeToExl_GW(ByteArrayOutputStream byteOs)throws Exception{
        try{
            int timeLength = 0;
            ITrendService trendService = new TrendServiceImpl();
            if(null != stepTime && !"".equals(stepTime)){
                timeLength = Integer.valueOf(stepTime);
            }
            gwjcList=trendService.querygwjcList(beginTime,endTime,timeLength);
            String filename="轨温监测历史查询"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())) + ".xls";
            exportFileName = new String(filename.getBytes("GBK"), "ISO-8859-1");
            WritableWorkbook wbook = Workbook.createWorkbook(byteOs); //建立excel文件
            WritableSheet wsheet = wbook.createSheet("轨温监测历史查询", 0); //工作表名称
            WritableFont wfont1 = new WritableFont(WritableFont.ARIAL, 9,      //初始化一些样式
                    WritableFont.NO_BOLD, false,
                    jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
            WritableFont wfont2 = new WritableFont(WritableFont.ARIAL, 10);
            WritableFont wfont3 = new WritableFont(WritableFont.ARIAL, 9,
                    WritableFont.NO_BOLD, false,
                    jxl.format.UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.AUTOMATIC);
            WritableFont wfont4 = new WritableFont(WritableFont.ARIAL, 10);
            WritableCellFormat titleFormat1 = new WritableCellFormat(wfont1);
            WritableCellFormat titleFormat2 = new WritableCellFormat(wfont2);
            WritableCellFormat titleFormat3 = new WritableCellFormat(wfont3);
            WritableCellFormat titleFormat3_1 = new WritableCellFormat(wfont3);
            WritableCellFormat titleFormat4 = new WritableCellFormat(wfont4);
            titleFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
            titleFormat1.setAlignment(Alignment.CENTRE);
            titleFormat1.setWrap(true); 

            titleFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            titleFormat2.setAlignment(Alignment.CENTRE);//设置居中对齐(这俩哪个是上下/左右对齐也没验证过)
            titleFormat2.setBackground(jxl.format.Colour.GREY_25_PERCENT);//单元格背景色
            titleFormat3.setVerticalAlignment(VerticalAlignment.CENTRE);
            titleFormat3.setAlignment(Alignment.CENTRE);
            titleFormat3.setWrap(true);//是否自动换行
            titleFormat3.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);//给单元格加边框          titleFormat3_1.setVerticalAlignment(VerticalAlignment.CENTRE);
            titleFormat3_1.setAlignment(Alignment.CENTRE);
            titleFormat3_1.setWrap(true);
            titleFormat3_1.setBackground(jxl.format.Colour.ORANGE);
            titleFormat3_1.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);//Biankuang
            titleFormat4.setVerticalAlignment(VerticalAlignment.CENTRE);
            titleFormat4.setAlignment(Alignment.CENTRE);
            titleFormat4.setBackground(jxl.format.Colour.GRAY_25);
            titleFormat4.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.HAIR);//Biankuang
            StringBuffer title = new StringBuffer();
            title.append("轨温监测查询条件:");
            title.append("开始时间 [");
            title.append(beginTime);
            title.append("]");
            title.append(" 结束时间:[");
            title.append(endTime);
            title.append("]");
            wsheet.setColumnView(0, 19);
            wsheet.setRowView(0, 600);
            wsheet.addCell(new Label(0,0,title.toString(),titleFormat1));
            if("1".equals(tdzt)){
                title.append(" 启用通道状态");
                //设置表头行合并单元格
                wsheet.mergeCells(0, 0, 9, 0);
                int rows =1;
                wsheet.addCell(new Label(0,rows,"时间",titleFormat4));
                wsheet.addCell(new Label(1,rows,"左温度(℃)",titleFormat4));
                wsheet.addCell(new Label(2,rows,"右温度(℃)",titleFormat4));
                wsheet.addCell(new Label(3,rows,"环温(℃)",titleFormat4));
                wsheet.addCell(new Label(4,rows,"通道一",titleFormat4));
                wsheet.addCell(new Label(5,rows,"通道二",titleFormat4));
                wsheet.addCell(new Label(6,rows,"通道三",titleFormat4));
                wsheet.addCell(new Label(7,rows,"通道四",titleFormat4));
                wsheet.addCell(new Label(8,rows,"通道五",titleFormat4));
                wsheet.addCell(new Label(9,rows,"通道六",titleFormat4)); 

                for(int i=0;i<gwjcList.size();i++){
                    wsheet.addCell(new Label(0,rows+1,gwjcList.get(i).getSj(),titleFormat3));
                    wsheet.addCell(new Label(1,rows+1,gwjcList.get(i).getZwd(),titleFormat3));
                    wsheet.addCell(new Label(2,rows+1,gwjcList.get(i).getYwd(),titleFormat3));
                    wsheet.addCell(new Label(3,rows+1,gwjcList.get(i).getHw(),titleFormat3));
                    wsheet.addCell(new Label(4,rows+1,Integer.valueOf(gwjcList.get(i).getTd1zt())==1?"正   常":"异常",Integer.valueOf(gwjcList.get(i).getTd1zt())==1?titleFormat3:titleFormat3_1));
                    wsheet.addCell(new Label(5,rows+1,Integer.valueOf(gwjcList.get(i).getTd2zt())==1?"正常":"异常",Integer.valueOf(gwjcList.get(i).getTd2zt())==1?titleFormat3:titleFormat3_1));
                    wsheet.addCell(new Label(6,rows+1,Integer.valueOf(gwjcList.get(i).getTd3zt())==1?"正常":"异常",Integer.valueOf(gwjcList.get(i).getTd3zt())==1?titleFormat3:titleFormat3_1));
                    wsheet.addCell(new Label(7,rows+1,Integer.valueOf(gwjcList.get(i).getTd4zt())==1?"正常":"异常",Integer.valueOf(gwjcList.get(i).getTd4zt())==1?titleFormat3:titleFormat3_1));
                    wsheet.addCell(new Label(8,rows+1,Integer.valueOf(gwjcList.get(i).getTd5zt())==1?"正常":"异常",Integer.valueOf(gwjcList.get(i).getTd5zt())==1?titleFormat3:titleFormat3_1));
                    wsheet.addCell(new Label(9,rows+1,Integer.valueOf(gwjcList.get(i).getTd6zt())==1?"正常":"异常",Integer.valueOf(gwjcList.get(i).getTd6zt())==1?titleFormat3:titleFormat3_1));
                    rows= rows +1;
                }
            }else{
                wsheet.mergeCells(0, 0, 3, 0);
                int rows =1;
                wsheet.addCell(new Label(0,rows,"时间",titleFormat4));
                wsheet.addCell(new Label(1,rows,"左温度(℃)",titleFormat4));
                wsheet.addCell(new Label(2,rows,"右温度(℃)",titleFormat4));
                wsheet.addCell(new Label(3,rows,"环温(℃)",titleFormat4));
                for(int i=0;i<gwjcList.size();i++){
                    wsheet.addCell(new Label(0,rows+1,gwjcList.get(i).getSj(),titleFormat3));
                    wsheet.addCell(new Label(1,rows+1,gwjcList.get(i).getZwd(),titleFormat3));
                    wsheet.addCell(new Label(2,rows+1,gwjcList.get(i).getYwd(),titleFormat3));
                    wsheet.addCell(new Label(3,rows+1,gwjcList.get(i).getHw(),titleFormat3));
                    rows = rows +1 ;
                }
            }
            contentType = "application/vnd.ms-excel";
            wbook.write();
            wbook.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public String exportGW(){
        try{
            ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
            writeToExl_GW(byteOs);
            byteOs.flush();
            excelStream = new ByteArrayInputStream(byteOs.toByteArray(),0,byteOs.toByteArray().length);
            byteOs.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return SUCCESS;
    }

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

原文地址:https://www.cnblogs.com/skinchqqhah/p/10351572.html

时间: 2024-10-09 02:10:24

jxl导出excel --- 居中 自动换行 边框 样式的相关文章

jxl导出Excel中需要跳过的的坑

正如上篇文章(JXL封装不能使用static关键字问题)所说,在jxl导出Excel时候如果频繁使用到WritableCellFormat去设置单元格的样式,这个时候经常会出现两种情况: 1.报警告:Warning:  Maximum number of format records exceeded.  Using default format.虽然说程序员一般不看警告,but这个警告讨厌了,他会把你超过限制的格式恢复成默认格式.所以必须解决. 2.报异常:java.lang.ArrayInd

jxl导出Excel文件

一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat;

用jxl导出Excel

断断续续,终于把这个简单的功能实现了. 本身花的有效时间也不多,但是陆续出现好多蛋疼的问题,归根结底是自己程序功底不深厚,尤其对Java流理解不透彻. 还好今天时间充足,磨得我背疼不已的时候,终于把它整完了. 这里首先介绍下背景:SSM框架(前端easyui).POI因为乱码问题被pass掉.五一假期又没有花时间,不得不赶紧换个API把他整出来. 功能大致是这样的: 点击按钮导出所有的(不经分页的)数据到Excel中. busiAccept.jsp 1 <%@ page language="

struts jxl导出Excel

struts2配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>

java中使用jxl导出Excel表格详细通用步骤

该方法一般接收两个参数,response和要导出的表格内容的list. 一般我们将数据库的数据查询出来在页面进行展示,根据用户需求,可能需要对页面数据进行导出. 此时只要将展示之前查询所得的数据放入session中备份一份,在调用导出方法时,从session中获取即可, 如果为后台直接导出,直接查询数据库后将结果传入即可,当然也可以在导出Excel方法中查询. 查询方法: // 获取查询结果存入session中        Object resultList = request.getAttr

转:jxl导出excel(合并单元格)

Demo 代码如下: 1 import java.io.*; 2 import jxl.*; 3 import jxl.format.UnderlineStyle; 4 import jxl.write.*; 5 public class CreateXLS { 6 public static void main(String args[]) { 7 try { 8 //打开文件 9 WritableWorkbook book= Workbook.createWorkbook(new File(

JXL导出Excel工具类

将Excel中的数据读取到List<Map<String, Object>>集合中 package com.mvc.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field;

用jxl导出excel报表

1.在项目中加入jxl.jar包 2.页面是一个from表单,也可以是一个a标签...,示例如下 1 <s:form action="claimVoucherStatistics_createDetailExcel.action" name="queryForm"> 2 <label for="time">年份:</label> 3 <s:property value="year"/&

Java中用JXL导出Excel代码详解

jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支持非常好,API是纯Java的, 并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件. 另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式.使用如下:搭建环境 将下载后的文件解包,得到jxl.jar,放入classpath,安装就完成了.