您的位置:首页 > 其它

报表的预览打印 jxl包

2010-09-09 19:43 225 查看
项目紧且成本小,问了问一些专业的报表工具,例如水晶报表,finereport等等,还是不便宜的。不过本人到觉Finereport不错,试了他的免费版,相当顺手,文档做的也不错,价格也还适中,具体价格不方便公布。不过没办法,还得用免费的,这就是免费和收费的区别啊。

先做一个Demo吧。

1、 项目要求 excel的样式从本地io文件读取,可选择,实现不同的样式。

于是我在创建样式文件pattern.xls。
根据选择,读取样式文件中的样式,控制生成文件。具体格式和样式就自己设定吧,我就不贴出来了,EXcel文件而已。

2、编写数据库,因为数据时直接从数据库中读取的。

3、就可以使用jxl包读取数据生成报表了

具体程序:

package test;

/**
* @author shiyanming
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;

public class NewJxlDemo {
public static void writeExcel(OutputStream os,int a) throws Exception {

//参数a是颜色控制参数

DataBase db = new DataBase();
ResultSet rs = null;
String strSQL = null;

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
// 创建表"研究院"
jxl.write.WritableSheet ws = wwb.createSheet("研究院", 0);

// 设置默认字体
jxl.write.WritableFont wfc = new jxl.write.WritableFont(
WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
// 设置title字体
jxl.write.WritableFont titleFont = new WritableFont(WritableFont.ARIAL,
14, WritableFont.NO_BOLD, false, UnderlineStyle.SINGLE,
jxl.format.Colour.BLACK);
// 设置时间字体
jxl.write.WritableFont datewfc = new jxl.write.WritableFont(
WritableFont.ARIAL, 10, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
// 设置落款字体
jxl.write.WritableFont endFont = new WritableFont(WritableFont.ARIAL,
10, WritableFont.BOLD);

// jxl.write.WritableCellFormat wcfFC = new
// jxl.write.WritableCellFormat(
// wfc);
// wcfFC.setBackground(jxl.format.Colour.RED);

// 设置左右上下边距
ws.getSettings().setLeftMargin(0.6);
ws.getSettings().setRightMargin(0.6);
ws.getSettings().setTopMargin(0.3);
ws.getSettings().setBottomMargin(0.3);

WritableCellFormat detFormat = new WritableCellFormat(wfc);
// 用于Number的格式
NumberFormat nf = new NumberFormat("#");
WritableCellFormat numFormat = new WritableCellFormat(wfc, nf);

WritableCellFormat firstFormat = new WritableCellFormat();
WritableCellFormat dataFormat = new WritableCellFormat();
WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
WritableCellFormat dateFormat = new WritableCellFormat(datewfc);// 时间格式
WritableCellFormat endFormat = new WritableCellFormat(endFont);// 落款样式
// WritableCellFormat backFormat = new WritableCellFormat(); // 背景色

// 标题行具体格式
firstFormat.setAlignment(jxl.format.Alignment.CENTRE);
firstFormat.setBackground(Colour.GRAY_25); // 背静色
firstFormat.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.DOUBLE, jxl.format.Colour.GRAY_50);

/* ******************************************************** */

/* 下面是根据要求读取已存在的单元格的样式,设置自己单元格的样式*/
/* 首先创建一个可读取的文件*/

String path1 = new String();
path1 = "D://Program Files//Apache Software Foundation//Tomcat 6.0//webapps//JxlDemoTest//xls//pattern" + ".xls";
File p = new File(path1);
Workbook book =Workbook.getWorkbook(p);
Sheet sheet=book.getSheet(0);
Cell cell1=sheet.getCell(a,0);
Colour colour =cell1.getCellFormat().getBackgroundColour();
/* ******************************************************** */

// 内容行具体格式
dataFormat.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.DOUBLE, jxl.format.Colour.GRAY_50);
//dataFormat.setBackground(Colour.LIGHT_GREEN); // 背静色
dataFormat.setBackground(colour); // 背静色
dataFormat.setAlignment(Alignment.CENTRE); // 设置对齐方式
dataFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置对齐方式
numFormat.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.DOUBLE, jxl.format.Colour.GRAY_50);
numFormat.setWrap(true);

// 背景色设置与实现
/*
* for (int x = 0; x < 7; x++) { for (int y = 2; y < 5; y++) {
* backFormat.setBackground(Colour.GRAY_25); // 背静色 Label mergelabel =
* new Label(x, y, "", backFormat); ws.addCell(mergelabel); } }
*/
// 写入图片 只能是png形式
File file2 = new File("C://logo1.png");
WritableImage image = new WritableImage(0, 0, 4, 2, file2);
ws.addImage(image);
// 设置title行高
ws.setRowView(2, 600);
// 合并单元格
ws.mergeCells(0, 2, 6, 2);

// title具体格式:
titleFormat.setAlignment(Alignment.CENTRE); // 设置对齐方式
titleFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置对齐方式
// titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
// Colour.LIGHT_BLUE);//设置边框的颜色和样式
// titleFormat.setBackground(Colour.GRAY_25); //背静色
jxl.write.Label labelC = new jxl.write.Label(0, 2, "DEMO表单",
titleFormat);
ws.addCell(labelC);

// 打印日期
java.util.Date myDate = new java.util.Date();
SimpleDateFormat matter = new SimpleDateFormat("yyyy年 MM月 dd日");
labelC = new jxl.write.Label(5, 3, matter.format(myDate), dateFormat);
ws.addCell(labelC);
// 打印ID号
labelC = new jxl.write.Label(0, 3, "ID:", dateFormat);
ws.addCell(labelC);
// ws.setColumnView(0,20);

//
labelC = new jxl.write.Label(0, 4, "编号", firstFormat);
ws.addCell(labelC);
ws.setColumnView(0, 5);

labelC = new jxl.write.Label(1, 4, "内容1", firstFormat);
ws.addCell(labelC);
ws.setColumnView(1, 15);

labelC = new jxl.write.Label(2, 4, "内容2", firstFormat);
ws.addCell(labelC);
ws.setColumnView(2, 6);

labelC = new jxl.write.Label(3, 4, "内容3", firstFormat);
ws.addCell(labelC);
ws.setColumnView(3, 10);

labelC = new jxl.write.Label(4, 4, "内容4", firstFormat);
ws.addCell(labelC);
ws.setColumnView(4, 10);

labelC = new jxl.write.Label(5, 4, "内容5", firstFormat);
ws.addCell(labelC);
ws.setColumnView(5, 10);

labelC = new jxl.write.Label(6, 4, "备注", firstFormat);
ws.addCell(labelC);
ws.setColumnView(6, 10);

// 数据库连接

strSQL = "SELECT * from demo";
rs = db.executeQuery(strSQL);
int y = 5;
int sum = 0;
while (rs.next()) {

System.out.println(rs.getString(1));
// 插入数据
int num = Integer.parseInt(rs.getString(1));
jxl.write.Number labelc0 = new jxl.write.Number(0, y, num,
dataFormat);
ws.addCell(labelc0);

String con1 = rs.getString(2);
jxl.write.Label labelc1 = new jxl.write.Label(1, y, con1,
dataFormat);
ws.addCell(labelc1);

int con2 = Integer.parseInt(rs.getString(3));
jxl.write.Number labelc2 = new jxl.write.Number(2, y, con2,
dataFormat);
ws.addCell(labelc2);

String con3 = rs.getString(4);
jxl.write.Label labelc3 = new jxl.write.Label(3, y, con3,
dataFormat);
ws.addCell(labelc3);

String con4 = rs.getString(5);
jxl.write.Label labelc4 = new jxl.write.Label(4, y, con4,
dataFormat);
ws.addCell(labelc4);

String con5 = rs.getString(6);
jxl.write.Label labelc5 = new jxl.write.Label(5, y, con5,
dataFormat);
ws.addCell(labelc5);

String con6 = rs.getString(7);
jxl.write.Label labelc6 = new jxl.write.Label(6, y, con6,
dataFormat);
ws.addCell(labelc6);

sum = sum + con2;
y++;

}
// 统计
jxl.write.Label labelsum = new jxl.write.Label(0, y, "统计", detFormat);
ws.addCell(labelsum);
jxl.write.Number f = new jxl.write.Number(2, y, sum, detFormat);
ws.addCell(f);
// 统计计算
// Formula f = new Formula(2, y, "SUM(C6:C14)", numFormat); //设置公式
// ws.addCell(f);

// 落款
jxl.write.Label end = new jxl.write.Label(4, y + 2, "落款:", endFormat);
ws.addCell(end);
jxl.write.Label endcont = new jxl.write.Label(5, y + 2, "xxxx",
endFormat);
ws.addCell(endcont);

//
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
// sql.close();
// con.close();
// return true;

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String path = new String();
// String path1 = new String();
path = "C://test3" + ".xls";
File f = new File(path);
f.createNewFile();
writeExcel(new FileOutputStream(f),1);
} catch (Exception e) {
e.printStackTrace();
System.out.println("输出Excel出现错误");
}

}

}

4、接着就要在页面上预览了。

<div align="center">

<iframe id="myExcelHtml" src="这里填生成Excel文件的位置。" width="600" height="350"
align="middle">
</iframe>
</div>

我用的是jsp,写了一个servlet,通过表单选择生成地址,来设置预览的地址。

5、再接着就是打印了

Excel的打印,使用Java打印服务API。调用java的javax.print包中的。打印服务实现了PrintService接口.通过调用接口中定义的createPrintJob()方法创建一个打印事件,作为DocPrintJob的一个实例。
打印的方法,我也是写进的servlet里,这里就只贴方法了。

private void printFileAction() {
// 构造一个文件选择器,默认为当前目录
// JFileChooser fileChooser = new
// JFileChooser(SystemProperties.USER_DIR);
// int state = fileChooser.showOpenDialog(this); //弹出文件选择对话框
String path = new String();
path = "D://Program Files//Apache Software Foundation//Tomcat 6.0//webapps//JxlDemoTest//xls//test3" + ".xls";
File f = new File(path);
// 构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookup
.lookupDefaultPrintService();
// 显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
try {
DocPrintJob job = service.createPrintJob(); // 创建打印作业
FileInputStream fis = new FileInputStream(f); // 构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();//指定文档属性 构造一个新的空哈希文档属性集
Doc doc = new SimpleDoc(fis, flavor, das); // 建立打印文件格式
job.print(doc, pras); // 进行文件的打印
} catch (Exception e) {
e.printStackTrace();
}
}

}

呵呵 虽有缺陷,但可以交差了。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: