使用aspx 直接生成excel


<%@ Page Language="C#" EnableEventValidation="false" ResponseEncoding="gb2312" ContentType="application/vnd.ms-excel" %>

<%@ Import Namespace="IriskingAttend.Web" %>
<%@ Import Namespace="System" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="application/vnd.ms-excel; charset=gb2312">
    <meta name="ProgId" content="Excel.Sheet">
    <meta name="Generator" content="Microsoft Excel 11">
</head>
<body>
    <% 
        Response.ContentType = "application/vnd.ms-excel"; Response.Charset = "utf-8"; Response.AddHeader("Content-Disposition", "inline; filename=" + "luanma.xls");
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
    %>
    <div>
        <table x:str border="1" cellpadding="0" cellspacing="0" style='border-collapse: collapse;
            text-align: center; table-layout: fixed; width: 1000pt; height: 102px;'>
            <%
                DateTime beginTime = Convert.ToDateTime(Request.QueryString["beginTime"]);
                DateTime endTime = Convert.ToDateTime(Request.QueryString["endTime"]);
                string departName = Request.QueryString["departName"];
                int length = (int)endTime.AddDays(1).Subtract(beginTime).TotalDays;
            %>
            <tr>
                <td colspan="<%=(length+15) %>" rowspan="2" align="center">
                    郭二庄月出勤明細報表 <span style='mso-spacerun: yes'> </span>
                    <%=beginTime.ToString("yyyy-MM-dd")%>
                    至
                    <%=endTime.ToString("yyyy-MM-dd")%> <span>部門:<%=departName %></span>
                </td>
            </tr>
            <tr>
                
            </tr>
            <tr>
                <td rowspan="2" align="center">
                    姓名
                </td>
                <td rowspan="2" align="center">
                    考勤號
                </td>
                <td rowspan="2" align="center">
                    工數
                </td>
                <td colspan="2" class="style2" align="center">
                    其中
                </td>
                <% for (DateTime mindate = beginTime; mindate <= endTime; mindate = mindate.AddDays(1))
                   { %>
                <td rowspan="2" class="style1" align="center">
                    <%=mindate.Day.ToString("d2")%>
                </td>
                <% } %>
                <td colspan="3" class="style1" align="center">
                    對比數據
                </td>
                <td colspan="2" class="style2" align="center">
                    對比后
                </td>
                <td colspan="2" class="style2" align="center">
                    有工數
                </td>
                <td rowspan="2" class="style2" align="center">
                    有工數
                </td>
                <td colspan="2" class="style2" align="center">
                    其中:夜班
                </td>
            </tr>
            <tr>
                <td rowspan="1" align="center">
                    工數
                </td>
                <td rowspan="1" align="center">
                    工數
                </td>
                <td rowspan="1" align="center">
                    工數
                </td>
                <td rowspan="1" align="center">
                    異常
                </td>
                <td colspan="1" align="center">
                    異常
                </td>
                <td rowspan="1" align="center">
                    工數
                </td>
                <td rowspan="1" align="center">
                    工數
                </td>
                <td rowspan="1" align="center">
                    有資
                </td>
                <td colspan="1" align="center">
                    有資
                </td>
                <td rowspan="1" align="center">
                    井上工數
                </td>
                <td rowspan="1" align="center">
                    井下工數
                </td>
            </tr>
            <%
                for (int i = 0; i < DomainServiceIriskingAttend._monthAttendList.Count; i++)
                {%>
            <tr height="19" style='height: 14.25pt'>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].PersonName%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].AttendSn%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].WorkConut%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].WorkCount%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].WorkCount%>
                </td>
                <% for (int j = 0; j < length; j++)
                   { %>
                <td>
                    <%= DomainServiceIriskingAttend._monthAttendList[i].DailySign[j]??"" %>
                </td>
                <% } %>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].NormalWorkCount%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].Exception%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].LocationException%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].AfterOn%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].AfterIn%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].YouZi%>
                </td>
                <td>
                    <%=DomainServiceIriskingAttend._monthAttendList[i].YouZi%>
                </td>
                <td class="style2">
                    <%=DomainServiceIriskingAttend._monthAttendList[i].WorkCountable%>
                </td>
                <td class="style1">
                    <%=DomainServiceIriskingAttend._monthAttendList[i].NightOn%>
                </td>
                <td class="style1">
                    <%=DomainServiceIriskingAttend._monthAttendList[i].NightIn%>
                </td>
            </tr>
            <%}%>
        </table>
    </div>
</body>
</html>

  //使用contentType 直接將輸出格式改成application/vnd.ms-xls, 頁面導向到這里直接下載。比較菜也是相對簡單的一種方法

//在IE下 中文文件名亂碼問題解決

 string filename = "中文名稱亂碼.xls";
        // IE瀏覽器亂碼問題,只能采用URLEncoder編碼  
        filename = HttpUtility.UrlEncode(filename, Encoding.UTF8);
        Response.ContentType = "application/vnd.ms-excel"; Response.Charset = "utf-8"; Response.AddHeader("Content-Disposition", "inline; filename=" + filename);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM