zoukankan      html  css  js  c++  java
  • winfrom把数据导出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;

  • 相关阅读:
    Educational Codeforces Round 6
    Educational Codeforces Round 5
    [Educational Round 5][Codeforces 616F. Expensive Strings]
    [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
    Codeforces Round #509 (Div. 2)
    从零开始编写一个vue插件
    Web前端错题模糊题记录
    vue中响应式props办法
    vue中router-link的click事件失效的解决办法
    ubuntu修改顶栏颜色
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2074399.html
Copyright © 2011-2022 走看看