jmesa 使用和导出中文excel,pdf数据处理
来源:互联网 发布:java中的init方法 编辑:程序博客网 时间:2024/05/22 07:46
做javaEE一个好的表单插件对开发是有帮助的!
Jmesa是ExtremeTable项目重新开发一个的Html表格组件,提供过滤、排序、分页、数据导出等功能。Jmesa非常易于使用,可按自己的需求进行扩展。JMesa需要JDK1.5以上的环境。
按照官方的办法:
Jmesa源码的包结构大致分为四个包:
org.jmesa.core Jmesa的核心包,所下面定义下最核心的接口CoreContext,CoreContext的作有容后再述,下面有四个子包:
org.jmesa.core.filter 过滤器(按条件对纪录进行过滤)
org.jmesa.core.sort 排序,该包提供纪录按指定列进行排序的功能
org.jmesa.core.message I18N资源文件实现,使用Java内置的ResourceBundle实现。较简单
org.jmesa.core.preference Jmesa选项,使用Java内置Properties实现。也较简单
org.jmesa.limit 该包提供对纪录进行过滤的功能,limit的原意为限制,这里表现为过滤。Jmesa提供接口让开发人员自定义存放Limit的地方。该接口就是位于该包 下面子包state下的State接口,用来保存当前用户查询选项或参数的状态,这些参数可能包括过滤条件(Filter),排序条件(Sort)以及分 页信息等。
org.jmesa.view 视图,亦即展现。该包下定义了展现表格的接口及提供了两种实现,CSV及HTML。展现的原理很简单,一个视图拥有一个被展现的组件(Component)和渲染器(Renderer)。使用渲染器来渲染组件。那么自然而然,View包下面又看到两个子包:
org.jmesa.view.component 定义了表格需要的组件,即表格,行,列。
org.jmesa.view.renderer 定义表格所需要的一系列渲染器,如行渲染,列渲染,单元格渲染等
org.jmesa.web 为在Web环境中使用Jmesa提供的一系列便利的类。
我们要使用它,先下载war 包,运行看看demo ,官网的例子是用标签,因为是开源的我们也可以自己封转他给前台一个list 就行,
让人头疼的是他导出pdf,文件和Excel标题对中文的支持:
先看看Excel标题的:
Excal 导出本身没有问题(poi 本身对中文支持好),但是标题是乱码,这个问题主要是他需要展示在Web 端标题。
首先要下载jmesa 的源码包:http://code.google.com/p/jmesa/downloads/list
现在最新的是3.0。
既然的导出那就先看org.jmesa.view.excel.ExcelViewExporter 这个类
- public ExcelViewExporter(View view, CoreContext coreContext, HttpServletResponse response, String fileName) {
- super(view, coreContext, response, fileName);
- }
public ExcelViewExporter(View view, CoreContext coreContext, HttpServletResponse response, String fileName) { super(view, coreContext, response, fileName); }
这句话是他处理导出文件,看来是父类处理的,OK那就改掉org.jmesa.view.AbstractViewExporter这个抽象类中的fileName,嗯他又交给了fn 响应那就直接改fn 转码:
这句话:
- String fn = new String(fileName.getBytes(encoding), encoding);
String fn = new String(fileName.getBytes(encoding), encoding);
改为:
- String fn = new String(fileName.getBytes("GB2312"), "ISO-8859-1");
String fn = new String(fileName.getBytes("GB2312"), "ISO-8859-1");
还有:
加上这句话:
- response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
如果是版本是2.3那就直接改
org.jmesa.view.excel.ExcelViewExporter这个类
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
fileName = new String(fileName.getBytes(), "ISO-8859-1");//修改
这样的话所有导出的标题乱码问题解决了。
下面看看导出PDF,java 世界导出pdf 的方案常有的有两个:Jasper Report 和
iText ,其实Jasper Report 是对 iText 的封装,iText 比较原始,对中文支持非常差。哎,恰恰jmesa 用的就是iText 封装的pdf 导出
我们使用jmesa 的pdfp 导出,
如果是2.4之前的jmesa 版本需要修改org.jmesa.view.pdfp.PdfPView
这个类的render()方法,具体修改方式为:
public PdfPTable render() { //增加的代码****************** Font cellFont = null; Font headerCellFont = null; if(fontName != null && fontEncoding != null){ try { BaseFont bfChinese = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); cellFont = new Font(bfChinese, 12F, Font.NORMAL); headerCellFont=new Font(bfChinese, 12F, Font.NORMAL,getHeaderFontColor()); } catch(Exception e) { e.printStackTrace(); } } if( null == cellFont){ cellFont = FontFactory.getFont("Helvetica", 12F, 0); headerCellFont = FontFactory.getFont("Helvetica", 12, Font.NORMAL, getHeaderFontColor()); } //***********增加的代码结束 PdfPTable pdfpTable = new PdfPTable(getTable().getRow().getColumns().size()); pdfpTable.setSpacingBefore(3); Row row = getTable().getRow(); List<Column> columns = row.getColumns(); // build table headers for (Iterator<Column> iter = columns.iterator(); iter.hasNext();) { Column column = iter.next(); //修改的代码 PdfPCell cell = new PdfPCell(new Paragraph(column.getTitle(),headerCellFont)); cell.setPadding(3.0f); cell.setBackgroundColor(getHeaderBackgroundColor()); pdfpTable.addCell(cell); } // build table body Collection<?> items = getCoreContext().getPageItems(); int rowcount = 0; for (Object item : items) { rowcount++; columns = row.getColumns(); for (Iterator<Column> iter = columns.iterator(); iter.hasNext();) { Column column = iter.next(); String property = column.getProperty(); Object value = column.getCellRenderer().getCellEditor().getValue(item, property, rowcount); //修改的代码 PdfPCell cell = new PdfPCell(new Paragraph(value == null ? "" : String.valueOf(value), cellFont)); cell.setPadding(3.0f); if (isRowEven(rowcount)) { cell.setBackgroundColor(getEvenCellBackgroundColor()); } else { cell.setBackgroundColor(getOddCellBackgroundColor()); } pdfpTable.addCell(cell); } } return pdfpTable; }
如果jmesa 版本是3.0 那更好了,他已经对font 进行了封装,只不过老外恶心屏蔽了乱码异常,找到getFontWithColor 这个方法,改造他:
BaseFont baseFont = createFont(fontName,fontEncoding,BaseFont.NOT_EMBEDDED);
改为
BaseFont baseFont = createFont("c:\\windows\\fonts\\simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
这个调用的是windows 自身的字体处理机制,别的字体 iText不支持啊,没办法就这么目前就这么办把,如果你是高手那就重写jmesa 导出文件的机制吧 哈哈~。
- jmesa 使用和导出中文excel,pdf数据处理
- bootstrap table和tableExport导出支持中文的Excel和pdf等表格
- jpivot 中输出 pdf打印中文、excel导出中文方法
- Jpivot mdx查询编辑器和Jpivot导出PDF、Excel中文乱码问题
- phpExcel导出到Excel和pdf
- java通过poi导出excel和pdf
- 导出excel、pdf
- 使用第三方组件NPOI和iTextSharp根据DataTable导出Excel、Word、Pdf
- 使用web程序导出Excel,PDF和CSV三种文件
- ireport导出中文pdf
- 导出到excel使用中文文件名
- html导出table支持(excel,png,word,pdf<pdf不太行,中文不支持>)
- 使用jasperreports制作报表(导出pdf excel html)
- 水晶报表导出为word,excel和pdf格式
- 水晶报表导出为word,excel和pdf格式
- as3xls导出Excel和解决导出中文乱码问题
- 使用PDFlib在PHP网页导出中文pdf文档
- 导出EXCEL,PDF 方法简单明了
- JavaScript实现动态添加,删除行
- 队的链式实现
- UVALive 4877 记忆搜索
- linux查找命令/find/locate/whereis/which/type
- HTTP协议详解
- jmesa 使用和导出中文excel,pdf数据处理
- 全面剖析XMLHttpRequest对象
- poj 3298 Antimonotonicity DP
- [usaco] 海明码
- 变量声明或定义在循环内外
- 一个简单易用的反编译工具jad
- java 注解
- 解决Android平台移植ffmpeg的一揽子问题
- Linux平台下使用JNI