用 HTML 格式导出 Excel 时,如何保留显示网格线


ASP.NET 中用 DataGrid 的 RenderControl 方法输出到 Excel,是一种常见的办法。其本质是用 html 语法编写 Excel, 表格用 table 表示。但是默认情形下会发现 Excel 的背景中的网格线(Gridlines) 不显示。用什么选项可以显示网格线呢?搜索了一下,答案是在 <head /> 中嵌入一段内容指示一些选项即可。

<html xmlns:x="urn:schemas-microsoft-com:office:excel">
    
<head>
        
<!--[if gte mso 9]><xml>
            <x:ExcelWorkbook>
                <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                        <x:Name>工作表标题</x:Name>
                        <x:WorksheetOptions>
                            <x:Print>
                                <x:ValidPrinterInfo />
                            </x:Print>
                        </x:WorksheetOptions>
                    </x:ExcelWorksheet>
                </x:ExcelWorksheets>
            </x:ExcelWorkbook>
        </xml>
        <![endif]
-->
    
</head>
    
<body>
        
<table border="1">
            
<tr>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
            
</tr>
            
<tr>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
            
</tr>
            
<tr>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
                
<td>test</td>
            
</tr>
        
</table>
    
</body>
</html>

原帖子地址在 http://forums.asp.net/1674188/ShowThread.aspx
(Export to Excel from ASP.Net with Excel Gridlines enabled)