Principales respuestas
LLenar un datatable

Pregunta
-
Tengo un boton que manda a gridview, pero no se como llenar el datatable
DataTable dt = new DataTable();
dt = getDetalle(ID);public void ExcelDownload(object sender, EventArgs e)
{
{
ulong i = 0;
DataTable dt = new DataTable();
dt = getDetalle(ID);
String filename = HttpContext.Current.User.Identity.Name + "_Formato_" + ".xlsx";
FileStream fs = new FileStream(Server.MapPath("~/templates/Bitacora.xlsx"), FileMode.Open, FileAccess.Read);
var pck = new OfficeOpenXml.ExcelPackage(fs);
i = 2;
ExcelWorksheet ws = pck.Workbook.Worksheets.First();
foreach (DataRow row in dt.Rows)
{
ws.Cells[$"A{i}"].Value = row["residuo"];
ws.Cells["A20"].Value = "holaaaaaaaaaaaaaaaaa";
i = i + 1;
}
fs.Close();
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}
Respuestas
-
Lo hice en el mismo evento onclick quedo asi, por si alguien le interesa
public void ExcelDownload(object sender, EventArgs e)
{
{
ulong i = 0;
DataTable dt = new DataTable();
//dt = getDetalle(ID);
if (GridView1.Rows.Count != 0)
{
//Forloop for header
for (int a = 1; a < GridView1.HeaderRow.Cells.Count; a++)
{
dt.Columns.Add(GridView1.HeaderRow.Cells[a].Text);
}
//foreach for datarow
foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 1; j < row.Cells.Count; j++)
{
dr[GridView1.HeaderRow.Cells[j].Text] = row.Cells[j].Text;
}
dt.Rows.Add(dr);
}
//Loop for footer
if (GridView1.FooterRow.Cells.Count != 0)
{
DataRow dr = dt.NewRow();
for (int a = 1; a < GridView1.FooterRow.Cells.Count; a++)
{
//You have to re-do the work if you did anything in databound for footer.
}
dt.Rows.Add(dr);
}
dt.TableName = "formato";
}
String filename = HttpContext.Current.User.Identity.Name + "_Formato_" + ".xlsx";
FileStream fs = new FileStream(Server.MapPath("~/templates/FORMTO.xlsx"), FileMode.Open, FileAccess.Read);
var pck = new OfficeOpenXml.ExcelPackage(fs);
i = 12;
ExcelWorksheet ws = pck.Workbook.Worksheets.First();
//ws.Cells["A20"].Value = "holaaaaaaaaaaaaaaaaa";
foreach (DataRow row in dt.Rows)
{
if (dt.Rows[0][24].ToString() == "CONDICION")
{
ws.Cells[$"E8"].Value = "holaaa";
ws.Cells[$"A{i}"].Value = row[1];
ws.Cells[$"B{i}"].Value = row[2];
ws.Cells[$"C{i}"].Value = row[3];
ws.Cells[$"D{i}"].Value = row[4];
ws.Cells[$"F{i}"].Value = row[6];
ws.Cells[$"G{i}"].Value = row[7];
ws.Cells[$"H{i}"].Value = row[8];
ws.Cells[$"I{i}"].Value = row[9];
ws.Cells[$"J{i}"].Value = row[10];
ws.Cells[$"K{i}"].Value = row[11];
ws.Cells[$"L{i}"].Value = row[12];
ws.Cells[$"M{i}"].Value = row[13];
ws.Cells[$"N{i}"].Value = row[14];
ws.Cells[$"O{i}"].Value = row[15];
ws.Cells[$"P{i}"].Value = row[16];
ws.Cells[$"Q{i}"].Value = row[17];
ws.Cells[$"R{i}"].Value = row[18];
ws.Cells[$"S{i}"].Value = row[19];
ws.Cells[$"T{i}"].Value = row[20];
ws.Cells[$"U{i}"].Value = row[21];
ws.Cells[$"V{i}"].Value = row[22];
ws.Cells[$"W{i}"].Value = row[23];
}
{
}
// ws.Cells["A20"].Value = "holaaaaaaaaaaaaaaaaa";
i = i + 1;
}
fs.Close();
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}- Marcado como respuesta danielglez lunes, 23 de septiembre de 2019 19:24
Todas las respuestas
-
hola
Planteas un problema con la carga de un DataTable y muestras todo el codigo menos donde implementas el getDetalle() para poder recomendar las modificaciones
cual es el codigo de getDetalle() ?
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina -
Lo hice en el mismo evento onclick quedo asi, por si alguien le interesa
public void ExcelDownload(object sender, EventArgs e)
{
{
ulong i = 0;
DataTable dt = new DataTable();
//dt = getDetalle(ID);
if (GridView1.Rows.Count != 0)
{
//Forloop for header
for (int a = 1; a < GridView1.HeaderRow.Cells.Count; a++)
{
dt.Columns.Add(GridView1.HeaderRow.Cells[a].Text);
}
//foreach for datarow
foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 1; j < row.Cells.Count; j++)
{
dr[GridView1.HeaderRow.Cells[j].Text] = row.Cells[j].Text;
}
dt.Rows.Add(dr);
}
//Loop for footer
if (GridView1.FooterRow.Cells.Count != 0)
{
DataRow dr = dt.NewRow();
for (int a = 1; a < GridView1.FooterRow.Cells.Count; a++)
{
//You have to re-do the work if you did anything in databound for footer.
}
dt.Rows.Add(dr);
}
dt.TableName = "formato";
}
String filename = HttpContext.Current.User.Identity.Name + "_Formato_" + ".xlsx";
FileStream fs = new FileStream(Server.MapPath("~/templates/FORMTO.xlsx"), FileMode.Open, FileAccess.Read);
var pck = new OfficeOpenXml.ExcelPackage(fs);
i = 12;
ExcelWorksheet ws = pck.Workbook.Worksheets.First();
//ws.Cells["A20"].Value = "holaaaaaaaaaaaaaaaaa";
foreach (DataRow row in dt.Rows)
{
if (dt.Rows[0][24].ToString() == "CONDICION")
{
ws.Cells[$"E8"].Value = "holaaa";
ws.Cells[$"A{i}"].Value = row[1];
ws.Cells[$"B{i}"].Value = row[2];
ws.Cells[$"C{i}"].Value = row[3];
ws.Cells[$"D{i}"].Value = row[4];
ws.Cells[$"F{i}"].Value = row[6];
ws.Cells[$"G{i}"].Value = row[7];
ws.Cells[$"H{i}"].Value = row[8];
ws.Cells[$"I{i}"].Value = row[9];
ws.Cells[$"J{i}"].Value = row[10];
ws.Cells[$"K{i}"].Value = row[11];
ws.Cells[$"L{i}"].Value = row[12];
ws.Cells[$"M{i}"].Value = row[13];
ws.Cells[$"N{i}"].Value = row[14];
ws.Cells[$"O{i}"].Value = row[15];
ws.Cells[$"P{i}"].Value = row[16];
ws.Cells[$"Q{i}"].Value = row[17];
ws.Cells[$"R{i}"].Value = row[18];
ws.Cells[$"S{i}"].Value = row[19];
ws.Cells[$"T{i}"].Value = row[20];
ws.Cells[$"U{i}"].Value = row[21];
ws.Cells[$"V{i}"].Value = row[22];
ws.Cells[$"W{i}"].Value = row[23];
}
{
}
// ws.Cells["A20"].Value = "holaaaaaaaaaaaaaaaaa";
i = i + 1;
}
fs.Close();
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}- Marcado como respuesta danielglez lunes, 23 de septiembre de 2019 19:24