2016-05-19 180 views
0

我正在使用GridView将表格导出到Excel。单元格填充不起作用

我有这个在我的控制器:

GridView gv = new GridView(); 

gv.DataSource = lstExportedExcel.ToList(); // lstExportedExcel does have stuff in it 
gv.DataBind(); 

gv.HeaderRow.Cells[0].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[1].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[2].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[3].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[4].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[5].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[6].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[7].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[8].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[9].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[10].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[11].BackColor = System.Drawing.Color.LightCyan; 
gv.HeaderRow.Cells[12].BackColor = System.Drawing.Color.LightCyan; 

gv.CellPadding = 10; //doesn't work 

Response.ClearContent(); 
Response.Buffer = true; 
Response.AddHeader("content-disposition", "attachment; filename=DailySummaryExport.xls"); 
Response.ContentType = "application/ms-excel"; 
Response.Charset = ""; 
StringWriter sw = new StringWriter(); 
HtmlTextWriter htw = new HtmlTextWriter(sw); 
gv.RenderControl(htw); 
Response.Output.Write(sw.ToString()); 
Response.Flush(); 
Response.End(); 

我已经做了研究这一点,很多解决方案都在前端,但我如何得到这个工作?当我打开Excel电子表格时,每个单元格中的文本都没有从默认边框获取填充。

任何帮助表示赞赏。

+0

您必须更加明确,试试这个: http://stackoverflow.com/questions/316931/how-to-define-cellpadding-in-gridview-in-asp- net – IrishChieftain

+0

@IrishChieftain我没有这个* gridview *的特定视图,这个gridview正在被创建,所以我将如何获得* CSS *的工作?GridView有一个* CssClassName *属性..但我失去了如何使这项工作 –

回答

0

工作盲目这里,但是这给了一枪:

添加到您的样式表:

.gv tr th 
{ 
    padding: 10px; 
} 

.gv tr td 
{ 
    padding: 10px; 
} 

然后动态地设置类代码:

gv.CssClass = "gv"; 

documentation

“GridView的同一列中的所有单元格con trol有相同的宽度。填充量应用于最宽的单元格,而列中的所有其他单元格都具有此单元格宽度。同样,同一行中的所有单元格都具有相同的高度。填充量应用于行中最高的单元格,并且该行中的所有其他单元格都具有此单元格高度。单个单元格大小无法指定。“

+0

对不起,但没有运气..我真的不明白为什么他们有一个* CellPadding *属性,当它不能有效地工作.. –

+0

看看这个,方法应该工作,如果你玩它: http://stackoverflow.com/questions/607547/define-global-site-style-for-aspgridview-with-plain-css-without-using-vs-skins – IrishChieftain