在表格上,我使用存储程序获取数据,我希望将这一数据输出到一个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");`
}#