gpt4 book ai didi

c# - EPPlus 生成的 Excel 电子表格

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:59 24 4
gpt4 key购买 nike

这让我很生气,我有一个带复选框的 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");

}
}
}
}

如有任何帮助,我们将不胜感激。

最佳答案

it doesnt allow me to use ws.Cells[1, iColumnCount] = c.ColumnName;

那一行应该是:

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

but it now falls over on the 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. can someone help me get the row/column count?

The .Dimension property gives the address for the range covering the top left cell to the bottom right cell so to get the row count we can use:

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

列数也类似:

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

关于c# - EPPlus 生成的 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com