C#中关于导出GridView数据到Excel

string saveFileName = "";
                SaveFileDialog savefile = new SaveFileDialog();
                savefile.DefaultExt = "xls";
                savefile.Filter = "Excel文件|*.xls";
                savefile.ShowDialog();
                saveFileName = savefile.FileName;
                if (saveFileName.IndexOf(":") < 0) return;
Excel.Application xlsapp = new Microsoft.Office.Interop.Excel.Application();
                object missing = System.Reflection.Missing.Value;
                if (xlsapp == null)
                {
                    MessageBox.Show("您的系统没有安装Excel,请您确认后再操作!");
                    return;
                }
                Excel.Workbooks workbooks = xlsapp.Workbooks;
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
                Excel.Range range;
                worksheet.Cells[1, 1] = vGridControl.Rows[0].Properties.Caption;
                for (int i = 0; i < vGridControl.Rows.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = vGridControl.Rows[i].Properties.Caption;
                    range = (Excel.Range)worksheet.Cells[1, i + 1];
                    range.Interior.ColorIndex = 15;
                    range.Font.Bold = true;
                }
                for (int i = 0; i < vGridControl.Rows.Count; i++)
                {
                    worksheet.Cells[2, i + 1] = vGridControl.Rows[i].Properties.Value;
                    range = (Excel.Range)worksheet.Cells[2, i + 1];
                    range.Interior.ColorIndex = 15;
                    range.Font.Bold = true;
                }
                //worksheet.Cells[3, 1];
                for (int i = 0; i < gViewDetail.Columns.Count; i++)
                {
                    worksheet.Cells[4, i + 1] = gViewDetail.Columns[i].Caption;
                    range = (Excel.Range)worksheet.Cells[4, i + 1];
                }
                for (int i = 0; i < connectionRate.Count; i++)
                {
                    worksheet.Cells[5 + i, 1] = connectionRate[i].Lac;
                    worksheet.Cells[5 + i, 2] = connectionRate[i].Ci;
                    worksheet.Cells[5 + i, 3] = connectionRate[i].ConnectionTimes;
                    worksheet.Cells[5 + i, 4] = connectionRate[i].ConnectionSuccessTimes;
                    worksheet.Cells[5 + i, 5] = connectionRate[i].ConnectionSuccessRate;
                    worksheet.Cells[5 + i, 6] = connectionRate[i].ConnectionUnSuccessTimes;
                    worksheet.Cells[5 + i, 7] = connectionRate[i].ConnectionUnSuccessRate;
                    worksheet.Cells[5 + i, 8] = "错误详细";
                    for (int j = 0; j < connectionRate[i].ErrorValueList.Count; j++)
                    {
                        worksheet.Cells[5 + i, 9+3*j] = connectionRate[i].ErrorValueList[j].Key;
                        worksheet.Cells[5 + i, 10+3*j] = connectionRate[i].ErrorValueList[j].DataValue;
                        worksheet.Cells[5 + i, 11+3*j] = connectionRate[i].ErrorValueList[j].DataRate;
                    }
                    range = (Excel.Range)worksheet.Cells[5 + i, 11 + 3*connectionRate[i].ErrorValueList.Count];
                }

                range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[connectionRate.Count + 4, vGridControl.Rows.Count + 11]);
                range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
                range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
                range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
                range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;

                worksheet._SaveAs(savefile.FileName, missing, missing, missing, missing, missing, missing, missing, missing);

                workbook.Close(missing, missing, missing);
                xlsapp.Quit();

 

 还有一种比较简单的方法:

string saveFileName = "";
                SaveFileDialog savefile = new SaveFileDialog();
                savefile.DefaultExt = "xls";
                savefile.Filter = "Excel文件|*.xls";
                savefile.ShowDialog();
                saveFileName = savefile.FileName;
                if (saveFileName.IndexOf(":") < 0) return;
                int members= tabCtrlLeft.SelectedTabPageIndex;
                switch(members)
                {
                    case 0:
                        {
                            vGridControl.ExportToXls(savefile.FileName);
                        }
                        break;
                    case 1:
                        {
                            gViewDetail.OptionsPrint.ExpandAllDetails = true;
                            gViewDetail.OptionsPrint.PrintDetails = true;
                            gViewDetail.ExportToXls(savefile.FileName);
                        }
                        break;
                    default:
                        MessageBox.Show("请选择要输出的数据页面!");
                        break;
                }

以上是个人在做c#时用的代码,其中用到的第三方插件是DevExpress中的gridview跟vgridcontrol,这个第三方的插件真的很强大,对于做图表啊什么的很是有帮助。

不过在导出到xls时还要注意就是using Excel = Microsoft.Office.Interop.Excel;在代码前要加上这个引用,还要在reference中add reference中添加com组件中的Microsoft Excel 12.0 Object Library.希望对大家有些帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值