aparecer datos a exccel
Publicado por Daniela (10 intervenciones) el 14/03/2018 15:56:12
hola, necesito que ayuden con como hago para que los datos del datagrid aparezcan en excel...solo me aparece el encabezado, en excel me aparce "Inherit"
este es el codigo que estoy usando
las imagenes son el datagrid que exportaré y el resultado de cuando exporto


este es el codigo que estoy usando
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
protected void btnGridviewToExcel_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application XcelApp = new Microsoft.Office.Interop.Excel.Application();
XcelApp.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < this.grvAnalisis.Columns.Count + 1; i++)
{
XcelApp.Cells[1, i] = this.grvAnalisis.Columns[i - 1].HeaderText;
}
for (int i = 0; i < this.grvAnalisis.Rows.Count; i++)
{
for (int j = 0; j < this.grvAnalisis.Columns.Count; j++)
{
XcelApp.Cells[i + 2, j + 1] = (this.grvAnalisis.Rows[i].Cells[j].ValidateRequestMode != null) ? this.grvAnalisis.Rows[i].Cells[j].ValidateRequestMode.ToString() : String.Empty;
grvAnalisis.EnableViewState = true;
}
}
XcelApp.Columns.AutoFit();
XcelApp.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.ContentEncoding = System.Text.ASCIIEncoding.UTF8;
Response.AddHeader("content-disposition", "attachment;filename=AnalisisDeCurva.xls");
//Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel;";
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
//Page page = new Page();
// Form HtmlForm = new Form();
Page.Controls.Add(Form);
Form.Controls.Add(grvAnalisis);
// Page.RenderControl(hw);
grvAnalisis.AllowPaging = true;
this.CargarData();
// grvAnalisis.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in grvAnalisis.HeaderRow.Cells)
{
cell.BackColor = grvAnalisis.HeaderStyle.BackColor;
}
foreach (GridViewRow row in grvAnalisis.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = grvAnalisis.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = grvAnalisis.RowStyle.BackColor;
cell.BackColor = Color.White;
}
cell.CssClass = "textmode";
}
}
//grvAnalisis.RenderControl(hw);
string style = @"<style> .textmode </style>";
Response.Charset = "iso-8859-1";
Response.Charset = "UTF-8";
//Response.ContentEncoding = Encoding.Default;
Response.Write(sw);
Response.Output.Write(sw.ToString());
// Response.Flush();
Response.End();
}
las imagenes son el datagrid que exportaré y el resultado de cuando exporto
Valora esta pregunta


0