从我的数据集中获取列名称

时间:2014-11-14 14:22:20

标签: sql asp.net dataset

我的代码在这里循环通过客户并将结果导出到具有多个工作表的Excel文档中,所有这些都取决于该客户有多少服务/报告。 我想要的只是获取列名称并将其打印到gridview中。 这是我的完整代码,我运行客户并将所有内容导出到excel文件(工作正常)。 我似乎完全迷失在这里,我确实需要一些帮助。

  var custName = Convert.ToString(Request.QueryString["cusName"]);
        try
        {             
            DataSet dt = new DataSet();             
            SqlConnection connection = new SqlConnection("Data Source=AP;Initial Catalog=DB_inf;User Id=sa;Password=*****;App=EntityFramework");
            using (SqlCommand sqlCmd = new SqlCommand("info.dbo.SP_Accounting_GetAll_Services", connection))
            {
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@customerName", SqlDbType.NVarChar).Value = custName;
                using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
                {
                    Session["TaskTable"] = dt;
                    sqlDa.Fill(dt);
                    if (dt.Tables.Count > 0)
                    {
                        MemoryStream ms = new MemoryStream();
                        int i = 1;
                        using (ExcelPackage package = new ExcelPackage(ms))
                        {
                            string filename = Server.MapPath("") + @"\Accounting_Templates\" + custName + ".xlsx";
                            FileInfo inputfile = new FileInfo(filename);
                            ExcelPackage summaryTemplate = new ExcelPackage(inputfile);
                            foreach (ExcelWorksheet sheet in summaryTemplate.Workbook.Worksheets)
                            {
                                package.Workbook.Worksheets.Add(sheet.Name, sheet);
                            }

                            foreach (DataTable table in dt.Tables)
                            {
                                var columns = new List<string>();


                                ExcelWorksheet worksheet = package.Workbook.Worksheets.ToList().Where(z => z.Name == string.Format("Report{0}", i)).FirstOrDefault();
                                if (worksheet != null)
                                {
                                    worksheet.Cells["A1"].LoadFromDataTable(table, true);
                                    worksheet.Cells.AutoFitColumns();
                                }
                                i++;
                            }
                            Response.Clear();
                            package.SaveAs(Response.OutputStream);
                            Response.AddHeader("content-disposition", "attachchment; filename=" + custName + ".xls");
                            Response.Charset = "";
                            Response.ContentType = "application/vnd.xls";
                            Response.End();
                        }
                   }
                }
            }
        }
        catch (Exception)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Something wen't wrong');", true);
        }

1 个答案:

答案 0 :(得分:0)

ExcelWorksheet worksheet = package.Workbook.Worksheets.ToList().Where(z => z.Name == string.Format("Report{0}", i)).FirstOrDefault();
if (worksheet != null)
{
    for (int colNo = 0; colNo < table.Columns.Count; colNo++)
    {
        worksheet.Cells[this.ColNumberToName(colNo) + "1"] = table.Columns[colNo].Name;
    }
    worksheet.Cells["A2"].LoadFromDataTable(table, true);
    worksheet.Cells.AutoFitColumns();
}
i++;

请注意,LoadFromDataTable现在进入&#34; A2&#34;而不是&#34; A1&#34;。

我留给你实施private string ColNumberToName(int colNo)。这将数字从0转换为A,B,c等