aspx导出Excel文件-转

来源:互联网 发布:wps数据有效性设置两个 编辑:程序博客网 时间:2024/06/04 10:26

private void DataSetToExcelXls(DataSet ds)
        {   
            用例说明#region 用例说明
            /**//*
            【输入】
             sender:      
             e:            
            【输出】
             无
            【流程定义】
             1、将生成的DataSet的数据导到指定的Execl文件里,并显示出来
            【可选流程】
                           
            【问题】
           
            */
            #endregion
           
            Random rd=new Random(int.Parse(DateTime.Now.ToString("MMddhhmmss")));
            string strFileName =DateTime.Now.ToString("yyyyMMdd")+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+txtBeginTime.Text.Trim()+txtFinishTime.Text.Trim()+rd.Next(999999).ToString()+ ".XLS";
           
            //Excel.Application excel= new Excel.ApplicationClass();//Excel.Application();
            Excel.Application excel ;
            //excel = new Excel.Application();
            excel = new Excel.ApplicationClass();
            Excel.WorkbookClass oWB;
            Excel.Worksheet oSheet;
            oWB = (Excel.WorkbookClass)(excel.Workbooks.Add(true));
            oSheet = (Excel.Worksheet)oWB.ActiveSheet;
           
            oSheet.Cells[1, 1] =lblReportTime.Text;    //合并项
            oSheet.Cells[2, 1] = lblReportCount.Text;//合并项
            oSheet.Cells[4, 1] = "话机号码" ;//合并项
            oSheet.Cells[4, 2] = "月份" ;//合并项
            oSheet.Cells[3, 3] = "通话总次数(次)";
            oSheet.Cells[3, 7] = "通话总时长(分)";
            oSheet.Cells[3,11] = "通话总金额(元)";
            oSheet.Cells[4, 3] = "国际";
            oSheet.Cells[4, 4] = "国内";
            oSheet.Cells[4, 5] = "港澳台";
            oSheet.Cells[4, 6] = "合计";
            oSheet.Cells[4, 7] = "国际";
            oSheet.Cells[4, 8] = "国内";
            oSheet.Cells[4, 9] = "港澳台";
            oSheet.Cells[4,10] = "合计";
            oSheet.Cells[4,11] = "国际";
            oSheet.Cells[4,12] = "国内";
            oSheet.Cells[4,13] = "港澳台";
            oSheet.Cells[4,14] = "合计";

            oSheet.get_Range("A1","N4").Font.Size= 9 ;
            oSheet.get_Range("A1","N4").Font.Bold = true;
            oSheet.get_Range("A1","N4").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
            oSheet.get_Range("A1","N4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
           
            //该范围内设置外面一层边框
            oSheet.get_Range("A1","N4").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;

            //该范围内设置里面所有单元格的边框
            oSheet.get_Range("A1","N4").Borders.Weight = Excel.XlBorderWeight.xlThin ;
            oSheet.get_Range("A1","N4").Borders.Color = Color.Black.ToArgb() ;
            //范围内单元格添加背景颜色
            oSheet.get_Range("A1","N2").Cells.Interior.Color =Color.FromArgb(153,255,204).ToArgb();
            oSheet.get_Range("A1","N2").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
       
            oSheet.get_Range("A3","N4").Cells.Interior.Color = Color.LemonChiffon.ToArgb();
            oSheet.get_Range("A3","N4").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
           
            DataTable dt =ds.Tables[0];
            DataRow dr;
            int count=dt.Rows.Count+5;
            string strExcelLength = "N"+count.ToString().Trim();

            int I,J;
            for(I = 5;I< count;I++)
            {               
                dr=dt.Rows[I-5];
               
                for(J = 1;J<(dt.Columns.Count +1) ;J++)
                {
                    oSheet.Cells[I,J] = dr[J-1].ToString().Trim() ;
               
                }
               
            }

            oSheet.get_Range("A5",strExcelLength).Font.Size= 9 ;
            oSheet.get_Range("A5",strExcelLength).Font.Bold = false;
            oSheet.get_Range("A5",strExcelLength).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
            oSheet.get_Range("A5",strExcelLength).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            //该范围内设置外面一层边框
            oSheet.get_Range("A5",strExcelLength).BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;

            //该范围内设置里面所有单元格的边框
            oSheet.get_Range("A5",strExcelLength).Borders.Weight = Excel.XlBorderWeight.xlThin ;
            oSheet.get_Range("A5",strExcelLength).Borders.Color = Color.Black.ToArgb() ;
            //范围内单元格添加背景颜色
            oSheet.get_Range("A5",strExcelLength).Cells.Interior.Color = Color.PaleTurquoise.ToArgb();
            oSheet.get_Range("A5",strExcelLength).Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;

           
            oSheet.get_Range("A1","N1").Merge(true);
            oSheet.get_Range("A2","N2").Merge(true);           

            oSheet.get_Range("A3","A4").Merge(true);
            oSheet.get_Range("B3","B4").Merge(true);
            oSheet.get_Range("C3","F3").Merge(true);
            oSheet.get_Range("G3","J3").Merge(true);
            oSheet.get_Range("K3","N3").Merge(true);


            string path=Server.MapPath(".") + "//..//..//excel-file//" + strFileName;           
           
            oWB.SaveAs(path,Missing.Value,"","",true,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,Missing.Value,Missing.Value);
           
            //oWB.SaveCopyAs(path);               
           
            oWB.Close(false,null,null);
            excel.Workbooks.Close();           
            excel.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);

            oSheet = null;
            oWB= null;
            excel = null;

            GC.Collect();       
                   
            string test="../../excel-file/" + strFileName;
            Response.Redirect(test);       
        }

原创粉丝点击