标题:C# Exl 导出报表
只看楼主
Issac_abc
Rank: 4
来 自:河南
等 级:业余侠客
威 望:2
帖 子:155
专家分:218
注 册:2010-6-17
结帖率:76.19%
 问题点数:0 回复次数:0 
C# Exl 导出报表
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using using Microsoft.Office.Interop.Excel;
using System.Drawing.Printing;
using System.Diagnostics;

 

namespace EPPALR05
{


       
    public partial class EPPALR05 : Form
    {
        private Microsoft.Office.Interop.Excel.Application m_objExcel = null;
        private Microsoft.Office.Interop.Excel.Workbooks m_objBooks = null;
        private Microsoft.Office.Interop.Excel._Workbook m_objBook = null;
        private Microsoft.Office.Interop.Excel.Sheets m_objSheets = null;
        private Microsoft.Office.Interop.Excel._Worksheet m_objSheet = null;
        private Microsoft.Office.Interop.Excel.Range m_objRange = null;
        //private Microsoft.Office.Interop.Excel.Font m_objFont = null;
        //private Microsoft.Office.Interop.Excel.QueryTables m_objQryTables = null;
        private Microsoft.Office.Interop.Excel._QueryTable m_objQryTable = null;
        // Frequenty-used variable for optional arguments.
        private object m_objOpt = System.Reflection.Missing.Value;    
        //DataBase-used variable
        private System.Data.OracleClient.OracleConnection OracleConn = null;
     
        private string strConnect = "Data Source=WZS_DEV;User Id=cms;Password =cms";
     
        private System.Data.OracleClient.OracleCommand OracleCmd = null;

         
        private int intSheetTotalSize = 0;//total record can divied sheet number
        private double dbTotalSize = 0;//record total number

        public EPPALR05()
        {
            InitializeComponent();
        }

       
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }


        private int GetTotalSize()
        {
            OracleConn = new System.Data.OracleClient.OracleConnection(strConnect);
            OracleCmd = new System.Data.OracleClient.OracleCommand("SELECT Count(*) FROM cmpps021 c021,cmpps071 c071 "+
                                                                   "WHERE c021.ncmp = c071.ncmp "+
                                                                   "AND c021.orno = c071.orno "+
                                                                   "AND c071.sorn IS NOT NULL", OracleConn);
            OracleConn.Open();

            dbTotalSize = Convert.ToInt32(OracleCmd.ExecuteScalar());
             OracleConn.Close();
            return 1;
        }
        private void DeclareExcelApp()
        {
            m_objExcel = new Microsoft.Office.Interop.Excel.Application();
            m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks;
            m_objBook = (Microsoft.Office.Interop.Excel._Workbook)(m_objBooks.Add(m_objOpt));
            m_objSheets = (Microsoft.Office.Interop.Excel.Sheets)m_objBook.Worksheets;

            intSheetTotalSize = GetTotalSize();        
          
            m_objExcel.Quit();
        }
        private void SaveExcelApp()
        {
            string excelFileName = string.Empty;
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "*.xls|*.*";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                excelFileName = sf.FileName;
            }
            else
            {
                return;
            }
            m_objBook.SaveAs(excelFileName, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt,
            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
            m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);        
          
        }
        private void ExportDataByQueryTable(int intSheetNumber, bool blIsMoreThan)
        {
            string strQuery = string.Empty;

            if (RdoPacking.Checked == true)
            {
                strQuery =
                       " SELECT" +
                       "   C021.NCMP, C021.HCUNO" +
                       " FROM" +
                       "   CMPPS021 C021, CMPPS071 C071" +
                       " WHERE" +
                       "   C021.NCMP = C071.NCMP" +
                       "   AND C021.ORNO = C071.ORNO" +
                       "   AND C071.SORN IS NOT NULL" +
                       "   AND C021.EPS_PAKNO = '" + txteps_pakno.Text.Trim() + "'";
            }
            else if (RdoContract.Checked == true)
            {           
                strQuery =
                       " SELECT" +
                       "   C021.NCMP, C021.HCUNO" +
                       " FROM" +
                       "   CMPPS021 C021, CMPPS071 C071, EPPALINV EINV, EPPALCON ECON" +
                       " WHERE" +
                       "   C021.NCMP = C071.NCMP" +
                       "   AND C021.ORNO = C071.ORNO" +
                       "   AND C071.SORN IS NOT NULL" +
                       "   AND C021.NCMP = " +
                       "   AND TO_CHAR (C021.PINO) = EINV.INVNO" +
                       "   AND ECON.PACKLST = EINV.PACKLST" +
                       "   AND ECON.CONTNO ='" + txteps_pakno.Text.Trim() + "'";
            }       
          
            m_objSheet = (Microsoft.Office.Interop.Excel._Worksheet)(m_objSheets.get_Item(intSheetNumber));

            m_objSheet.get_Range("A1", m_objOpt).set_Value(m_objOpt, "");

            m_objSheet.get_Range("B1", m_objOpt).set_Value(m_objOpt, "");

            m_objSheet.get_Range("C1", m_objOpt).set_Value(m_objOpt, "");

            m_objSheet.get_Range("D1", m_objOpt).set_Value(m_objOpt, "");

            m_objSheet.get_Range("E1", m_objOpt).set_Value(m_objOpt, "");

          
            m_objRange = m_objSheet.get_Range("A2", m_objOpt);
          
            m_objQryTable = m_objSheet.QueryTables.Add("OLEDB;Provider=OracleOLEDB.1;" + strConnect, m_objRange, strQuery);

            m_objQryTable.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertEntireRows;
            m_objQryTable.FieldNames = false;
            m_objQryTable.Refresh(false); //重新从数据源中读取当条记录
           
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DeclareExcelApp();
            SaveExcelApp(); 
        }      
    
    }
}
搜索更多相关主题的帖子: 报表 
2012-04-01 11:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-364987-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.182578 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved