由EPPlus生成的Excel电子表格

时间:2012-08-10 09:13:35

标签: c# epplus

这开始让我发疯,我有一个带有复选框的asp:gridview,用户可以检查他/她想要导出哪些信息到excel,当他们点击按钮时执行下面的代码,现在你可以看到我为gridview等中的每一行做了一个

如果检查了行的复选框,我会转到数据库执行一些信息返回一个数据表,然后尝试将其值添加到Epplus Excel电子表格中,但是在foreach(datacolum)foreach(DataRow)中它不允许我使用

ws.Cells[1, iColumnCount] = c.ColumnName; as it says its read only?

但是这个excel电子表格可能有1到10个不同的信息位,具体取决于检查了多少个复选框....有人可以帮助我,让我摆脱痛苦........: (

继承我的完整代码

protected void BtnTest_Click(object sender, EventArgs e)
{
    bool ReportGenerated = false;

    FileInfo newFile = new FileInfo("C:\\Users\\Scott.Atkinson\\Desktop\\book1.xls");
    ExcelPackage pck = new ExcelPackage(newFile);
    foreach (GridViewRow row in gvPerformanceResult.Rows)
    {
        object misValue = System.Reflection.Missing.Value;
        CheckBox chkExcel = (CheckBox)row.FindControl("chkExportToExcel");
        if (chkExcel.Checked)
        {

            HyperLink HypCreatedBy = (HyperLink)row.FindControl("HyperCreatedBy"); //Find the name of Sales agent

            string CreatedBy = HypCreatedBy.Text;
            string Fname = HypCreatedBy.Text;
            string[] names = Fname.Split();
            CreatedBy = names[0];
            CreatedBy = CreatedBy + "." + names[1];

            WebUser objUser = new WebUser(CreatedBy, true);

            DataTable DT = new DataTable();
            LeadOpportunities objLeadOpportunities = new LeadOpportunities();
            DT = objLeadOpportunities.LoadPRCDetail("PRC", objUser.ShortAbbr, objUser.CanViewAllLead, ReportCriteria); // Load the information to export to Excel.

            if (DT.Rows.Count > 0)
            {
                ReportGenerated = true;
                //Add the Content sheet
                var ws = pck.Workbook.Worksheets.Add("Content");
                ws.View.ShowGridLines = true;

                int iRowCount = ws.Dimension.Start.Row; //Counts how many rows have been used in the Excel Spreadsheet
                int iColumnCount = ws.Dimension.Start.Column; //Counts how many Columns have been used.

                if (iRowCount > 1)
                    iRowCount = iRowCount + 2;
                else
                    iRowCount = 1;

                iColumnCount = 0;

                foreach (DataColumn c in DT.Columns)
                {
                    iColumnCount++;
                    if (iRowCount == 0)
                        ws.Cells[1, iColumnCount] = c.ColumnName;
                    else
                        ws.Cells[iRowCount, iColumnCount] = c.ColumnName;
                }

                foreach (DataRow r in DT.Rows)
                {
                    iRowCount++;
                    iColumnCount = 0;
                    foreach (DataColumn c in DT.Columns)
                    {
                        iColumnCount++;
                        if (iRowCount == 1)
                            ws.Cells[iRowCount + 1, iColumnCount] = r[c.ColumnName].ToString();
                        else
                            ws.Cells[iRowCount, iColumnCount] = r[c.ColumnName].ToString();

                        WorkSheet.Columns.AutoFit(); //Correct the width of the columns
                    }
                }

                pck.Save();
                System.Diagnostics.Process.Start("C:\\Users\\Scott.Atkinson\\Desktop\\book1.xls");

            }
        }
    }
}

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

  

它不允许我使用   ws.Cells[1, iColumnCount] = c.ColumnName;

该行应该是:

ws.Cells[1,iColumnCount].Value = c.ColumnName
  

但它现在落在了int iRowCount = ws.Dimension.Start.Row; //计算Excel电子表格中使用的行数iColumnCount = ws.Dimension.Start.Column; //计算使用了多少列。有人可以帮助我获得行数/列数吗?

.Dimension属性将覆盖左上角单元格的范围的地址提供给右下角的单元格,以便获得我们可以使用的行数:

var rowCount = ws.Dimension.End.Row - ws.Dimension.Start.Row + 1;

和列数类似:

var colCount = ws.Dimension.End.Column - ws.Dimension.Start.Column + 1;