关于将HTML导出到Excel中时样式的问题

最近要把HTML页面中的Table导出到Excel中去,可是Table的样式没有在Excel中显示出来,后来百度了一下,也买发现解决办法,大部分都提到了NPOI插件去解决,但是因为时间少没时间去看,后来自己尝试了把导出到Excel中的字符串写入文本看了下,然后在复制到一个html页面才发现是样式的问题。最后使用行内样式解决了问题。


html页面的代码:

 <table id="datatable" class="panelContentTopTable" width="100%" runat="server">
        <tr>
            <td class="gridBorder" style="padding: 3px 3px 3px 3px;">
                <cc1:CustRepeater ID="CustRepeater1" runat="server" OnItemDataBound="CustRepeater1_ItemDataBound">
                    <HeaderTemplate>
                        <!--开始-->
                        <table id="datatable" width="100%" cellpadding="0" cellspacing="0" style="border-width: 0px;
                            border-left: solid 1px #94D4F8; border-top: #94D4F8 1px solid; width: 100%;">
                            <tr style="text-align: center; font-weight: bold; text-align: center; background: #D2F1FD;">
                                <td rowspan="2" style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    单位名称
                                </td>
                                <td rowspan="2" style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    设备在线
                                </td>
                                <td rowspan="2" style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    设备离线
                                </td>
                                <td colspan="5" style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    当前故障情况
                                </td>
                            </tr>
                            <tr style="text-align: center; font-weight: bold; text-align: center; background: #D2F1FD;">
                                <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    待受理
                                </td>
                                <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    待完工
                                </td>
                                <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    待程序检测
                                </td>
                                <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    待人工确认
                                </td>
                                <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                    报修总数
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr style="text-align: center;">
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="lblCountTypeName" Text='<%# Eval("CountTypeName").ToString() %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="CameraOnline" Text='<%# setnotnull(Eval("EquOnlineCount").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="CameraOffline" Text='<%# setnotnull(Eval("EquOfflineCount").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="lblRepairForResponse" Text='<%# setnotnull(Eval("RepairForResponse").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                    height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="OnResponse" Text='<%# setnotnull(Eval("RepairForWorkFinish").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="OnRepair" Text='<%# setnotnull(Eval("RepairForProgramCheck").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="ForAudit" Text='<%# setnotnull(Eval("RepairForManualCheck").ToString()) %>'
                                    runat="server" />
                            </td>
                            <td style="padding: 2px; border-right: solid 1px #94D4F8; border-bottom: solid 1px #94D4F8;
                                height: 22px; font-size: 13px;">
                                <cc1:CustLabel ID="Appeal" Text='<%# setnotnull(Eval("RepCount").ToString()) %>'
                                    runat="server" />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        <%--<tr style="text-align: center;">
                            <td>
                                合计
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                            <td>
                                0
                            </td>
                        </tr>--%>
                        </table>
                        <!--结束-->
                    </FooterTemplate>
                </cc1:CustRepeater>
            </td>
        </tr>
    </table>
后台导出代码:

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

其实只需要把以前写在class里面的样式直接写成行内样式就可以解决了,下面是页面效果

Excel中的效果:



发布了20 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/mt122/article/details/8085631