我在下面提到了将数据导出到 excel 中的代码
响应。清除();响应缓冲区=真;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Table tb = new Table();
GridView gv=new GridView();
tb.GridLines = gv.GridLines;
tb.BackColor = System.Drawing.Color.Transparent;
TableRow tr1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Controls.Add(BalanceSheetListControl2);
tr1.Cells.Add(cell1);
tb.Rows.Add(tr1);
tb.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; backgroud-color:transparent !important; } </style>";
Response.Write(style);
System.IO.StringReader writer = new System.IO.StringReader(sw.ToString());
string s=string.Empty;
string readString = string.Empty;
bool StartFlag = false;
StringBuilder build = new StringBuilder();
while((readString=writer.ReadLine())!=null)
{
// readString = writer.ReadLine().Trim();
s = readString;
Regex search_string = new Regex("<div.*?>([^<]+)</div>");
Match match = search_string.Match(s);
string section = match.Groups[1].Value.ToString();
if (section.Trim()=="-")
{
s = s.Replace(">-<", "><div align='right'>-</div><");
// s = s.Replace("-", " - ");
}
if (readString.Trim().Contains("main") || readString.Trim().Contains("column c"))
{
s=s+"</td><td>";
}
build.Append(s);
}
Response.Output.Write(build.ToString());
Response.Flush();
Response.End();
我必须在 Excel 中显示网格线,因为我使用了以下代码
GridView gv=new GridView();
tb.GridLines = gv.GridLines;
使用上面的代码后,excel只会生成垂直网格线,我还需要水平线,谁能帮我实现这个?提前致谢
作为参考,我附上了我当前 Excel 的图像