这是一个gridview导出到Excel的代码方法

    static public string 导出Excel(GridView gv)
    {
        string FilePath = HttpContext.Current.Request.PhysicalApplicationPath + @"dataResource\TempFile\OutputDataExcel.xlsx";
        // ArrayList VisCol = new ArrayList();
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
        Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
        Microsoft.Office.Interop.Excel.Range ra;
        
        int index_row, index_col;

        int jump = 0;

        for (index_col = 0; index_col < gv.HeaderRow.Cells.Count; index_col++)
        {
            ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + 1];

            if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
                gv.HeaderRow.Cells[index_col].Controls[0] is LinkButton)
                ra.set_Value(null,
            ((LinkButton)gv.HeaderRow.Cells[index_col].Controls[0]).Text
            );
            else if (gv.HeaderRow.Cells[index_col].Text.Trim() != string.Empty)
                ra.set_Value(null,
            gv.HeaderRow.Cells[index_col].Text
            );


            if (gv.HeaderRow.Cells[index_col].Controls.Count > 0 &&
                gv.HeaderRow.Cells[index_col].Controls[0] is Table)
            {

                //写入的位置
                //if (index_col + jump + 1 <= ws.Cells.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToRight).Column)
                ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, index_col + jump + 1];



                //读取的位置
                Table ta = (Table)gv.HeaderRow.Cells[index_col].Controls[0];
                if (index_col <= gv.HeaderRow.Cells.Count - 13 / 2)
                {

                    Microsoft.Office.Interop.Excel.Label la = ta.Rows.Count > 0 && ta.Rows[0].Cells.Count > 0 && ta.Rows[0].Cells[0].Controls.Count > 0 ? (ta.Rows[0].Cells[0].Controls[0] as Microsoft.Office.Interop.Excel.Label) : null;
                    if (la != null)
                        ra.set_Value(null,
                  ((Microsoft.Office.Interop.Excel.Label)ta.Rows[0].Cells[0].Controls[0]).Text
                  );
                }

                jump++;
            }
        }

        for (index_row = 0; index_row < gv.Rows.Count; index_row++)
        {
            for (index_col = 0; index_col < gv.Rows[index_row].Cells.Count; index_col++)
            {
                ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];


                ra.set_Value(null,
                    gv.Rows[index_row].Cells[index_col].Text.Replace("&#165;", "")
                    //gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim("&nbsp;".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
                    /*"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"*/
                );
                //System.Windows.Forms.MessageBox.Show(((Label)gv.Rows[index_row].Cells[index_col].Controls[0]).Text);
                //ra.set_Value(null, gv.Rows[index_row].Cells[index_col].Text.Replace("<br/>", "\n").Trim("&nbsp;".ToCharArray()).Trim("#165;".ToCharArray()));
            }
        }


        GridViewRow gvr = gv.FooterRow;
        for (index_col = 0; index_col < gvr.Cells.Count; index_col++)
        {

            ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[index_row + 2, index_col + 1];
            decimal _Temp;

            if (decimal.TryParse(gvr.Cells[index_col].Text.Replace("&nbsp;", "").Replace("&#165;", ""), System.Globalization.NumberStyles.AllowCurrencySymbol, null, out _Temp))

                ra.set_Value(null,
                    _Temp
                    //gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim("&nbsp;".ToCharArray()).Trim("#165;".ToCharArray())//"#165;"
                );
            else

                ra.set_Value(null, gvr.Cells[index_col].Text.Replace("<br/>", "\n").Trim("&nbsp;".ToCharArray()));
        }


        //    ra = ws.UsedRange.get_End(Microsoft.Office.Interop.Excel.XlDirection.xlDown);/*最后一行*/
        //    ra = (Microsoft.Office.Interop.Excel.Range)ws.Cells[ra.Row + 1/*最后一行加一行,也就是新行*/, 3];

        //    //System.Windows.Forms.MessageBox.Show(gvCon.Rows[85].Cells[3].Text);
        //    //ra.set_Value(null, "测试的值");

        if (System.IO.File.Exists(FilePath)) System.IO.File.Delete(FilePath);

        wb.Close(true, FilePath, null);
        app.Quit();
        string _return = (HttpContext.Current.Request.ApplicationPath == "/" ? string.Empty : HttpContext.Current.Request.ApplicationPath) + "/dataResource/TempFile/OutputDataExcel.xlsx";
        GC.Collect();
        return _return;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值