これは私を怒らせ始めています、私はチェックボックス付きのasp:gridviewを持っています、ユーザーは彼/彼女がどの情報をExcelにエクスポートしたいかをチェックすることができます、彼らがボタンをクリックすると以下のコードが実行されます、今あなたはグリッドビューなどの各行に対してimが実行しているのを見ることができます
行のチェックボックスがオンになっている場合は、DBに移動して情報を実行し、データテーブルを返し、その値をEpplus Excelスプレッドシートに追加しようとしますが、ではforeach(datacolum)
使用foreach(DataRow)
できません
ws.Cells[1, iColumnCount] = c.ColumnName; as it says its read only?
しかし、この1つの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");
}
}
}
}
どんな助けでも大歓迎です。