【问题标题】:Get column names from my dataset从我的数据集中获取列名
【发布时间】:2015-01-11 23:15:59
【问题描述】:

我的代码在此处循环访问客户并将结果导出到包含多个工作表的 Excel 文档中,这一切都取决于该客户拥有多少服务/报告。 我想要的是只获取列名并将它们打印到网格视图中。 这是我运行客户并将所有内容导出到 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);
        }

【问题讨论】:

    标签: sql asp.net dataset


    【解决方案1】:
    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 现在进入“A2”,而不是“A1”。

    我让你来实现private string ColNumberToName(int colNo)。这会将数字从 0 转换为 A、B、c 等。

    【讨论】:

    • 非常感谢 simon 我在度假自动取款机,我回来后会尝试您的解决方案! :-)
    猜你喜欢
    • 2014-05-22
    • 2015-03-19
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多