winfrom把数据导出Excel表格

本文介绍了一个使用C#和Microsoft Office Interop Excel库导出数据到Excel文件的示例。该示例展示了如何创建一个新的Excel工作簿,设置列名,并填充数据行。此外,还介绍了如何预先关闭可能存在的Excel进程。

#region 导出Excel
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOutExcel_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
                {
                    if (thisproc.ProcessName.Equals("EXCEL"))
                    {
                        thisproc.Kill();   //终止程序
                    }
                }


                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

                System.Data.DataTable ds;
                DataTableChangeDt();
                ds = newtb;


                int row_index, col_index;
                row_index = 1;
                col_index = 1;

                excel.Workbooks.Add(true);

                if (ds.Columns.Contains("tsiid"))
                {
                    ds.Columns.Remove(ds.Columns["tsiid"]);
                }
                if (ds.Columns.Contains("tsc_id"))
                {
                    ds.Columns.Remove(ds.Columns["tsc_id"]);
                }
                if (ds.Columns.Contains("tcs_id"))
                {
                    ds.Columns.Remove(ds.Columns["tcs_id"]);
                }
                ////导出Excle后显示的列名
                for (int i = 0; i < SumColumnsOld.Length; i++)
                {
                    excel.Cells[1, i + 1] = SumColumnsOld[i].ToString();
                }
                excel.Cells[1, SumColumnsOld.Length + 1] = "总计";
                //excel.Cells[1, 2] = "学员卡号";
                //excel.Cells[1, 3] = "班级类型";
                //excel.Cells[1, 4] = "学员班级";
                //excel.Cells[1, 5] = "发卡金额";
                //excel.Cells[1, 6] = "开始时间";
                //excel.Cells[1, 7] = "结束时间";
                //excel.Cells[1, 8] = "剩余次数";
                //excel.Cells[1, 9] = "状态";
                //excel.Cells[1, 10] = "教练";
                //excel.Cells[1, 11] = "是否结算";
                foreach (DataRow dr in ds.Rows)
                {
                    col_index = 0;

                    foreach (DataColumn dc in ds.Columns)
                    {

                        excel.Cells[row_index + 1, col_index + 1] = dr[dc];

                        col_index++;

                        System.Windows.Forms.Application.DoEvents();
                    }

                    row_index++;

                }
                excel.Visible = true;
            }
            catch { }
        }

//using Microsoft.Office.Interop.Excel;

转载于:https://www.cnblogs.com/wuhuisheng/archive/2011/06/07/2074399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值