package nc.ui.fh.des.excel;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.bill.BillItem;
import nc.ui.pub.report.ReportBaseClass;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
/**
* 报表导出Excel的工具类
* @author 麻杰<br>
* @创建时间:2011-11-16 下午06:08:23
*/
public class ExportExcelReport {
protected static javax.swing.JFileChooser m_chooser = null;
protected static ReportBaseClass reportUIBase;
/**
* @author 麻杰<br>
* 日期:2011-11-17
* @param tp 父组件
* @param sheetName Excel页签名称
* @param istotal 是否合计
*/
public static void exportExcel(ReportBaseClass tp ,String sheetName,boolean istotal){
reportUIBase = tp;
String filePath = null;
if (getChooser().showSaveDialog(tp) == javax.swing.JFileChooser.CANCEL_OPTION) {
return;
}
filePath = getChooser().getSelectedFile().toString();
if (filePath == null) {
MessageDialog.showHintDlg(tp, "警告", "请输入文件名保存!");
return;
}
if (filePath.indexOf(".xls") < 0) {
filePath = filePath + ".xls";
}
boolean isSucceed = writeJxlByTableModel(filePath,sheetName,istotal);
if(isSucceed){
MessageDialog.showHintDlg(tp, "提示", "导出完成");
}else{
MessageDialog.showHintDlg(tp, "提示", "导出失败");
}
}
/**
* 生成Excel文件
* @author 麻杰<br>
* 日期:2011-11-17
* @param filePatch
* @param sheetName
* @param istotal 是否合计
* @return
*/
public static boolean writeJxlByTableModel(String filePath,String sheetName,boolean istotal) {
JTable table = reportUIBase.getBillTable();
if (table == null || table.getRowCount() <= 0)
return false;
WritableWorkbook writableWorkbook = null;
OutputStream os = null;
try {
os = new FileOutputStream(filePath);
// 创建可写簿
writableWorkbook = Workbook.createWorkbook(os);
// 创建工作表
WritableSheet ws = writableWorkbook.createSheet(sheetName, 0);
// 创建一个内容 第一个整数为 列,第二个整数位 行
Label label = null;
int rows = table.getRowCount();
int cols = table.getColumnCount();
for (int row = 0; row < rows + 1; row++) {
for (int col = 0; col < cols ; col++) {
if(row == 0){
String headerName = table.getTableHeader().getColumnModel().getColumn(col).getHeaderValue() == null ? "" :
table.getTableHeader().getColumnModel().getColumn(col).getHeaderValue().toString();
label = new Label(col,row,headerName,getHeaderFormat());
ws.addCell(label);
ws.setColumnView(col, headerName == null ? 10 : (headerName.length() > 2 ? headerName.length()*3 : headerName.length()*6));
}
label = new Label(col,row+1,table.getValueAt(row, col) == null ? "" : table.getValueAt(row, col).toString());
if(label.getContents() != null){
if(label.getContents().equals("false")){
label.setString("N");
}else if(label.getContents().equals("true")){
label.setString("Y");
}
}
ws.addCell(label);
}
}
if(istotal){
//添加合计行
Map<Integer,String> map = getTotalMap();
if(map != null && map.size()>0){
Set<Entry<Integer,String>> set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Entry<Integer,String> entry = (Entry<Integer, String>) it.next();
label = new Label(entry.getKey(),rows+1,entry.getValue());
ws.addCell(label);
}
}
}
writableWorkbook.write();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
if(writableWorkbook != null){
try {
writableWorkbook.close();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 获得文件选择器
* @author 麻杰<br>
* 日期:2011-11-17
* @return
*/
private static javax.swing.JFileChooser getChooser() {
if (m_chooser == null) {
m_chooser = new JFileChooser();
m_chooser.setDialogType(JFileChooser.SAVE_DIALOG);
}
return m_chooser;
}
/**
* 获得表头行样式
* @author 麻杰<br>
* 日期:2011-11-16
* @throws WriteException
*/
private static WritableCellFormat getHeaderFormat() throws WriteException{
WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false);
WritableCellFormat format = new WritableCellFormat(font);
format.setWrap(false);
format.setAlignment(Alignment.CENTRE);//水平方向剧中
format.setVerticalAlignment(VerticalAlignment.CENTRE);
return format;
}
/**
* 获得合计封装Map
* @author 麻杰<br>
* 日期:2011-11-18
* @return
*/
private static Map<Integer,String> getTotalMap(){
Vector vector = (Vector)reportUIBase.getTotalTableModel().getDataVector().get(0);
Object total = null;
Map<Integer,String> map = new HashMap<Integer,String>();
for(int i=0;i<vector.size();i++){
total = vector.get(i);
if(total != null){
int result = getOrder(i);
if(result != -1){
map.put(result, total.toString());
}
}
}
return map;
}
/**
* 根据合计值的位置获得其在UITable中的位置
* @author 麻杰<br>
* 日期:2011-11-18
* @param num
* @return
*/
private static Integer getOrder(int num){
int result = 0;
BillItem[] billItem = reportUIBase.getBodyItems();
if(!billItem[num].isShow()){
return -1;
}
for(int i=0;i<num;i++){
if(billItem[i].isShow()){
result++;
}
}
return result;
}
}
相关推荐
《NC56报表开发PPT》是一份详细阐述NC报表开发流程和技术的文档,主要针对在企业信息系统中遇到的报表开发挑战,如大量的开发工作、难以复用的业务模型、频繁的个性化需求等问题,提出了全面的解决方案。文档的核心...
通过以上步骤,企业可以有效地将用友NC财务软件中的数据导出至Excel,以便进行更深入的分析、报表制作和决策支持。值得注意的是,导出数据前应确保所有必要的权限已获取,并且备份原始数据以防意外情况。此外,定期...
使用这类工具时,用户需要按照软件的指引,选择要转换的A文件,然后指定输出的Excel文件路径。在转换过程中,软件会自动识别A文件中的数据结构,将其转化为Excel可理解的表格形式。 转换后的Excel文件,用户可以...
1. **模板设计**:根据需求设计报表的布局和样式,这通常涉及Excel或其他报表设计工具。 2. **基类使用**:选择合适的基类,根据业务逻辑重写或扩展方法。 3. **数据获取**:连接到NC5X系统,通过API或SQL查询获取...
- **详细说明**:NC提供了专门的报表设计器工具,开发者需要先安装该工具并熟悉其基本操作。通过该工具,可以轻松地创建各种类型的报表,并对报表进行精细化布局。 - **7.1.2 查询模型** - **知识点**:查询条件...
- 数据导出:支持将费用汇总表导出为Excel等格式,便于进一步分析。 ### 二、报表管理模块 报表管理模块主要用于生成各种财务报表,如资产负债表、利润表等,帮助企业更好地掌握自身的财务状况。 - **新增功能**...
5. 系统中带有“打印”功能的地方都可导出Excel表格,通过预览功能实现。 **四、客户化设置** 1. **人员信息**:部门由集团统一设置,人员档案增加时需填写完整蓝色字体的信息,发放工资的人员还需录入银行账户信息...
5. 可以将任何有“打印”功能的地方导出为Excel表格,预览后点击左上角的按钮完成导出。 **四、客户化设置** 1. **人员信息**:部门由集团统一设定,不可修改。增加人员信息时,填写完整蓝色字体的项目,如有工资...