NC 5导出Excel
Excel导出功能 NC中功能事件代码:
@Override
protected void onBoRefresh() throws Exception {
UIFileChooser fc = new UIFileChooser();//文件选择器
fc.setDialogType(UIFileChooser.SAVE_DIALOG);// 指示 UIFileChooser 支持 "Save" 文件操作的类型值。
// fc.setFileSelectionMode(UIFileChooser.FILES_AND_DIRECTORIES);//指示显示文件和目录。
fc.setFileSelectionMode(UIFileChooser.FILES_ONLY);//指示仅显示文件。
fc.setFileFilter(new ExcelFileFilter());// 设置当前文件过滤器。
fc.setMultiSelectionEnabled(false);//设置文件选择器,以允许选择多个文件。
int i = fc.showSaveDialog(this.getBillUI()); //弹出一个 "Save File" 文件选择器对话框。
// fc.showOpenDialog(this.getBillUI()); //弹出一个 "Open File" 文件选择器对话框。
if(i == UIFileChooser.APPROVE_OPTION){
String filePatch = fc.getSelectedFile().getPath();
BillCardPanel cardPanel = this.getBillCardPanelWrapper().getBillCardPanel();
Boolean bool = ExportExcel.writeJxlByTableModel(filePatch, cardPanel);
if(bool){
MessageDialog.showHintDlg(this.getBillUI(), "提示", "导出信息成功!");
return;
}
}else{
System.out.println("撤销成功");
}
}
文件过滤类:
package nc.ui.ldzl.order; import java.io.File; import javax.swing.filechooser.FileFilter; /**
* @author sj
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class ExcelFileFilter extends FileFilter {
private String filters = "xls";
// private String filters2 = "xlsx";
private String description = "Microsoft Excel (*.xls)";
/**
* MyFileFilter 构造子注解。
*/
public ExcelFileFilter() {
super();
}
/**
* Whether the given file is accepted by this filter.
*/
public boolean accept(java.io.File f)//此过滤器是否接受给定的文件。
{
if (f != null)
{
if (f.isDirectory())//测试此抽象路径名表示的文件是否是一个目录。
{
return true;
}
String extension = getExtension(f);
if(extension != null && extension.trim().equals(this.filters))
{
return true;
}
}
return false;
}
/**
* The description of this filter. For example: "JPG and GIF Images"
* @see FileView#getName
*/
public String getDescription()//此过滤器的描述。
{
return description;
} public static String getExtension(File f) {
if(f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if(i>0 && i<filename.length()-1) {
return filename.substring(i+1).toLowerCase();
};
}
return null;
}
}
导出Excel代码类:
package nc.ui.pub.util; import java.io.*;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillScrollPane.BillTable;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
/**
* 用来导出Excel的工具
*/
public class ExportExcel { public static boolean writeJxlByTableModel(String filePatch,BillCardPanel cardPanel) {
BillTable table = (BillTable) cardPanel.getBillTable();
WritableWorkbook writableWorkbook = null;
OutputStream os = null;
if (table == null || table.getRowCount() <= 0) {
return false;
}
if (!filePatch.endsWith(".xls")) {
System.out.println("=======不是正确的xls格式,请核查==========");
return false;
}
try {
os = new FileOutputStream(filePatch);
// 创建可写簿
writableWorkbook = Workbook.createWorkbook(os);
// 创建工作表
WritableSheet ws = writableWorkbook.createSheet("楼号档案页签", 0);
// 创建一个内容 第一个整数为 列,第二个整数为 行
Label label = null;
int rows = table.getRowCount();
int cols = table.getColumnCount() - 1;
String[] headitem = new String[] { "vbillno", "pactname","reserve4" };//表头字段
for (int row = 0; row < rows + 1; row++) {
for (int col = 0; col < headitem.length; col++) {
if (row == 0) {
String headerName = cardPanel.getHeadItem(headitem[col]).getName();
label = new Label(col, row, headerName);
ws.addCell(label);
ws.setColumnView(col,headerName == null ? 10: (headerName.length() > 2 ? headerName.length() * 3 : headerName.length() * 6));
} else {
label = new Label(col, row, cardPanel.getHeadItem(headitem[col]).getValue());
ws.addCell(label);
}
}
for (int col = headitem.length; col < cols + headitem.length; col++) {
if (row == 0) {
String headerName = table.getTableHeader().getColumnModel().getColumn(col - headitem.length).getHeaderValue() == null ? "":
table.getTableHeader().getColumnModel().getColumn(col - headitem.length).getHeaderValue().toString();
label = new Label(col, row, headerName);
ws.addCell(label);
ws.setColumnView(col,headerName == null ? 10: (headerName.length() > 2 ? headerName.length() * 3 : headerName.length() * 6));
} else {
label = new Label(col, row, table.getValueAt(row - 1,col - headitem.length) == null ? "" : table.getValueAt(row - 1, col - headitem.length).toString());
ws.addCell(label);
}
}
}
writableWorkbook.write();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
if(writableWorkbook != null){
writableWorkbook.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
}
NC 5导出Excel的更多相关文章
- 导出excel时,以form方式提交json数据
今天在写项目时写到一个excel的导出,开始想用ajax请求后台后导出,但发现ajax会有返回值,而且ajax无法直接输出文件,而后台的excel导出方法已经封装好,不方便修改. 就改用了提交的方式f ...
- JQGrid 导出Excel 获取筛选条件
需求描述:页面加载后,进行相关数据搜索,要求点击导出按钮后 下载Excel文件. 思路:希望在点击[导出Excel]按钮时,获取到表格搜索时的filters内容. 在百度.api.jqgrid.js ...
- 杂项收集,包括-发邮件、二维码生成、文件下载、压缩、导出excel
本篇就最近工作解决的问题做个代码收集.包括以下几个方面:发邮件.二维码生成.文件下载.压缩.导出excel.有一种可用的解决方法就好,不求全面,不求原理. 1.发邮件: 命名空间:System.Net ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- 利用poi导出Excel
import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.r ...
- [django]数据导出excel升级强化版(很强大!)
不多说了,原理采用xlwt导出excel文件,所谓的强化版指的是实现在网页上选择一定条件导出对应的数据 之前我的博文出过这类文章,但只是实现导出数据,这次左思右想,再加上网上的搜索,终于找出方法实现条 ...
- NPOI导出Excel
using System;using System.Collections.Generic;using System.Linq;using System.Text;#region NPOIusing ...
- ASP.NET Core 导入导出Excel xlsx 文件
ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Li ...
- asp.net DataTable导出Excel 自定义列名
1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...
随机推荐
- 利用STM32CubeMX来生成USB_HID_Mouse工程【添加ADC】(2)【非dma和中断方式】
上回讲到怎么采集一路的adc的数据,这次我们来采集两路的数据. 现在直接修改原先的代码 /* Private variables ----------------------------------- ...
- 【WebLogic使用】1.WebLogic的下载与安装
一.WebLogic的介绍 WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...
- [ZZ] 基于Matlab的标记分水岭分割算法
基于Matlab的标记分水岭分割算法 http://blog.sina.com.cn/s/blog_725866260100rz7x.html 1 综述 Separating touching obj ...
- 1、根"/"目录结构
1.目录结构 FSH [root@localhost /]# tree -L . ├── bin -> usr/bin #普通用户使用的命令 ├── boot #存放系统启动相关文件,例如ker ...
- mysql 拒绝登录解决
一大早打开Navicat Lite for MySQL客户端,提示1045 access denied for user ’root’@’localhost’ using password yes,太 ...
- QQ第三方登录(完结篇)
书接上回,上回说到:这篇是代码篇 首先我们先来看一下我的母鹿(目录)吧 Connect2.1 是我们从下载的SDK,内容包含 其他文件在配置之后全部删除了! index.html 是我们点击登陆的页 ...
- Oracle不能并行直接添加主键的方法:先建唯一索引后建主键
环境:Oracle 11.2.0.3 需求:生产一张表由于前期设计不当,没有主键.现需要添加主键,数据量很大,想并行建立. 1.直接添加,提示ora-3001:未实施的功能;只能单线程建立主键 SQL ...
- 1732157 - Collecting diagnosis information for SAP HANA [VIDEO]
Symptom SAP Support asked you to provide a collection of the relevant diagnosis files (also known as ...
- 序列化与反序列化之Kryo
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. 需要序列化的情况: 当你想把的内存中的对象状态保存到一个文件中或者数据库中时候: 当你想 ...
- Oracle 关联查询
select count(1),a.policy_id from gp_pol_prod a where a.product_id=8401 group by a.policy_id having c ...