English 中文(简体)
c# [封闭]
原标题:exporting data from a stored procedure into an excel template file on c# [closed]
  • 时间:2024-01-04 01:31:34
  •  标签:
  • c#
Closed. This question needs details or clarity. It is not currently accepting answers.

在表格上,我使用存储程序获取数据,我希望将这一数据输出到一个Excel模板文档中,但我发现这一错误。

System.Data.DuplicateNameException:  A column named  Str1  already belongs to this DataTable. 
        private void btnExport_Click(object sender, EventArgs e)
        {
          
            SqlConnection myConn = new SqlConnection(Funtions.GetConnectString());
            myConn.Open();
            SqlCommand cmd = new SqlCommand("sp_export_MX_NEW", myConn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@StartDate", dtStartDate.DateTime);
            cmd.Parameters.AddWithValue("@EndDate", dt.EndDate.DateTime);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            gridControl1.DataSource = dt;
            dt.Columns.Add("Str1");
            dt.Columns["Str1"].AutoIncrement = true;
            dt.Columns["Str1"].AutoIncrementSeed = -1;
            dt.Columns["Str1"].AutoIncrementStep = 1;
            dt.Columns["Str1"].ReadOnly = true;
            dt.TableName = "productdetails";

            var info = new DataTable();
            info.Columns.Add("NameCompany");
            info.Rows.Add("Address");
            info.TableName = "info";
            var ds = new DataSet();
            ds.Tables.Add(dt);
            ds.Tables.Add(info);
            TemplateExcel.FillReport("invoice.xlsx", "template.xlsx", ds, new string[] { "{", "}" });
            Process.Start("invoice.xlsx");`
        }#
问题回答

根据这一错误,<代码>dt.Load(cmd.ExecuteReader();创建了一个表格,其中已经有“str1”栏。

因此,当<代码>dt.Columns.Add(“Str1”)运行时,该编码就留下了这一错误。

我不知道内容是什么,但你应当检查你的储存程序“第_export_MX_NEW”,看看它会产生何种表格和(或)栏目。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签