日期:2014-05-18  浏览次数:20983 次

C# excel导出报表
C#   excel导出报表,把查询到的数据循环插入到excel(模板)中,最好给个例子,谢谢!

------解决方案--------------------
google: NPOI
------解决方案--------------------
C# code
private void button1_Click(object sender, System.EventArgs e)
{
            EI.EIInfo inBlock = new EI.EIInfo();
            //EI.EIInfo outBlock = new EI.EIInfo();
            string Sql1 = "select * from tdmst01";
            inBlock.SetColName(1, "sql1");
            inBlock.SetColVal(1, "sql1", Sql1);
            EI.EIInfo outBlock = EI.EITuxedo.CallService("pccf02_qry", inBlock);
            Microsoft.Office.Interop.Excel.Application ep ;
            Microsoft.Office.Interop.Excel.Workbook ew ;
            Microsoft.Office.Interop.Excel.Worksheet ews ;
           
           
            //outBlock.GetBlockVal(efGrid1);
            //object filename = "@C:\\Documents and Settings\\Administrator\\桌面\\aaa.xls";
            object MissingValue = Type.Missing;
            string strKeyWord = "";    //指定要搜索的文本,如果有多个,则声明string[]
            object oText = strKeyWord.Trim().ToUpper();
            //Microsoft.Office.Interop.Excel.Range oRange;
            try
            {
             ep = new Microsoft.Office.Interop.Excel.ApplicationClass();
             ew = ep.Workbooks.Open(@"D:\\aaa.xls", MissingValue,
                        MissingValue, MissingValue, MissingValue,
                        MissingValue, MissingValue, MissingValue,
                        MissingValue, MissingValue, MissingValue,
                        MissingValue, MissingValue, MissingValue,
                        MissingValue);
             ews = (Microsoft.Office.Interop.Excel.Worksheet)ew.Worksheets[1];
            //Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
           
        //Workbook wBook   = excel.Workbooks.Add(true); 
        //Worksheet wSheet = (Microsoft.Office.Interop.Excel.Worksheet)ew.ActiveSheet;    
            //oRange = ((Microsoft.Office.Interop.Excel.Range)ews.UsedRange).Find(
            //    oText, MissingValue, MissingValue,
            //    MissingValue, MissingValue, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext,
            //    MissingValue, MissingValue, MissingValue);
        //Microsoft.Office.Interop.Excel.Range rng3;
            //Microsoft.Office.Interop.Excel.Range rng3 = ews.get_Range("C6", Missing.Value);
            //rng3.Value2 = "Hello"; 
            
            int i, j;
            for (j = 1; j <= outBlock.blk_info[0].Col; j++)
            {
                ews.Cells[1, j] = outBlock.GetColName(j);
            }
            for (i = 1; i <= outBlock.blk_info[0].Row; i++)
            {
                for (j = 1; j <= outBlock.blk_info[0].Col; j++)
                {
                    ews.Cells[i+1, j] = outBlock.GetColVal(i, j);
                }
            }
            
            //ews.Cells[2, 1] = "test8: "; 
                    //excel.Cells[4, 5] = "本公司传真: " ; 
                    //excel.Cells[5, 5] = "联系人: " ; 
                    //excel.Cells[4, 1] = "客户: " ; 
                    //excel.Cells[5, 1] = "联系人: " ; 
                    //excel.Cells[3, 8] = "户名:"; 
                    ////excel.Cells[3, 9] = AccountName; 
                    //excel.Cells[4, 8] = "开户行:"; 
                    ////excel.Cells[4, 9] = BranchName; 
                    //excel.Cells[5, 8] = "帐号:"; 
                    //excel.Cells[5, 9] = "’" + AccountID; 
                       //设置禁止弹出保存和覆盖的询问提示框 
        ep.DisplayAlerts = false;
        ep.AlertBeforeOverwriting = false; 
            //保存工作薄 
            //  wBook.Save(); 
            //每次保存激活的表,这样才能多次操作保存不同的Excel表,默认保存位置是在”我的文档"