且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#将数据导出到Excel时出现问题

更新时间:2023-02-15 07:49:06

在没有自动化Excel的情况下,您实际上不应使用服务器端代码来做到这一点(无论如何也可能会遇到权限问题),这是一种实现这是使用上面类似的代码,但是输出有效的HTML并以.xls扩展名保存.

这将使您可以加粗某些列并应用常规的HTML样式.

例如,在此处查看示例

http://forums.asp.net/t/1214938.aspx [
Without automating Excel, which you shouldn''t really do using server side code (and may run into permissions issues anyway), a way to achieve this is to use the similar code above but output valid HTML and save with the .xls extension.

This will let you bold certain columns + apply general HTML styling.

e.g look at examples here

http://forums.asp.net/t/1214938.aspx[^]

For example, create a new text file on your computer. Paste the following HTML into it & save the file. Then change the file extension to .xls & open it in Excel. Notice we''ve got bold headers + a ''red'' style.

<HTML>
<HEAD>
<style type='text/css'>
    table
    {
        cellpadding: 0px;
        font-size: xx-small;
        border-collapse:collapse;
    }
    td.some-style
    {
        border: 1px solid black;
        color: red;
    }
</style>
</HEAD>
<BODY>
<TABLE border=0>
        <TR>
        <Td></Td>
        <Td></Td>
        <Td></Td>
        <Td class="some-style">This is an example</Td>
        <Td></Td>
     </TR>
    <thead>
        <TR>
        <Th> ProductID</Th>
        <Th> Product Name</Th>
        <Th> Sales Person</Th>
        <Th> Date Closed</Th>
        <Th> Sale Amount</Th>
        </TR>
    </thead>
    <TR>
        <TD>1</TD>
        <TD>Code Project</TD>
        <TD>Dylan Morley</TD>
        <TD>10/03/2010</TD>
        <TD>50.00</TD>
    </TR>
    <TR>
        <TD>2</TD>
        <TD>Microsoft</TD>
        <TD>Dylan Morley</TD>
        <TD>10/03/2010</TD>
        <TD>50.00</TD>
    </TR>
</TABLE>
</BODY>
</HTML>




您可以在导出例程中轻松创建此HTML


注意:这仅是对于安装了Excel 2003或更高版本的计算机的有效解决方案,我认为这是何时首次引入HTML功能?




You could create this HTML quite easily in your export routine


NB: This would only be a valid solution for machines with Excel 2003 or greater installed, which I think is when then first introduced HTML capabilities?