gpt4 book ai didi

.net - 将GridView导出到Excel而不丢失Excel中的网格线

转载 作者:行者123 更新时间:2023-12-02 09:23:50 25 4
gpt4 key购买 nike

我有一个 GridView,我想将其导出到 Excel。当我使用网上找到的示例代码时,它可以将内容导出到 Excel,但由于某种原因,它还会清除导出表格之外的所有网格线。

对于普通 Excel 用户来说,这很容易修复,但我需要这个解决方案适用于每个人。

那么有没有一种方法可以将GridView中的数据导出到Excel工作簿中,使其看起来就像刚刚在Excel中输入的一样?我已经粘贴了下面使用的代码,假设存在一个名为 toPrint 的 GridView 并且具有准确的数据。

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + name + "_Registration_Forms.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Page.EnableViewState = false;

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

toPrint.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();

编辑:找到一个部分解决方案。如果我导出为逗号分隔列表并将标题设置为 CSV 文件,则它可以正常打开并且显示所有网格线(甚至是导出数据之外的网格线)。当然,唯一的问题是在导出它们之前必须从我的值中删除每个逗号和换行符。

最佳答案

下面的帖子给了我答案。 http://forums.asp.net/t/1074110.aspx 我将总结如何处理代码。

System.IO.StringWriter dvWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter OutputGenerator = new System.Web.UI.HtmlTextWriter(dvWriter);

//To put in the excel gridlines
dvWriter.Write(@"<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">");
dvWriter.Write(@"<head>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:WorksheetOptions>
<x:Panes></x:Panes>
<x:Print><x:Gridlines /></x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
</head>");
//*******Put your Table Body here *******

关于.net - 将GridView导出到Excel而不丢失Excel中的网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2376204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com