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;
}
}

  

04-13 11:12
查看更多