使用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);