62,047
社区成员
发帖
与我相关
我的任务
分享
Protected Sub btnPrintExcel_Click(sender As Object, e As EventArgs) Handles btnPrintExcel.Click
Dim ds As DataSet = '返回一个DataSet
Dim table1 As DataTable = ds.Tables(4)
Dim printDate As String
printDate = Now.ToString("D") 'Monday, November 08, 2010
printDate = Now.ToString("MMM d,yyyy") 'Nov 8, 2010
printDate = Now.ToString("d-MMM-yy") 'Nov 8, 2010
printDate += "; " & Now.ToString("t") '4:47 PM
Dim text As String = "<div align=" & Chr(34) & "Center" & Chr(34) & "> <font style='FONT-SIZE: 12pt; FONT-WEIGHT: bold;' >LINC Home Study - Additional Conversatin List</font> Print Date: " & printDate & "</div>"
Response.ClearContent()
Response.ClearHeaders()
Response.Clear()
Response.AddHeader("content-disposition", "filename=AddConWaitList.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
'Dim g As New DataGrid()
Dim g As New GridView()
g.DataSource = table1
g.DataBind()
Response.Write(text)
'Change the header row back color
g.HeaderStyle.BackColor = Color.White
For i As Integer = 0 To g.Rows.Count - 1
Dim r As GridViewRow = g.Rows(i)
r.Attributes.Add("class", "textmode")
'If (i Mod 2) <> 0 Then
' r.Cells(0).Style.Add("background-color", "#C2D69B")
'End If
'r.Cells(3).Style.Add("text-align", "center")
'r.Cells(4).Style.Add("text-align", "center")
'r.Cells(5).Style.Add("text-align", "center")
'r.Cells(7).Style.Add("text-align", "center")
'r.Cells(8).Style.Add("text-align", "center")
Next
g.RenderControl(htmlWrite)
'style to format numbers to string.
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style> "
Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(stringWrite.ToString()))
Response.Write(style)
Response.End()
Response.Flush()
End Sub
xlApp.Visible = True
xlSheet.Application.Visible = True
这两句去掉,改成保存为文件,然后读取这个文件用Response输出。 public ActionResult DataStatisticsDayListExcel()
{
DateTime DateStart = Request.QueryString["DateStart"].ToDateTime() == DateTime.MinValue ? DateTime.Now : Request.QueryString["DateStart"].ToDateTime(); ;
DateTime DateEnd = Request.QueryString["DateEnd"].ToDateTime() == DateTime.MinValue ? DateTime.Now.AddDays(1) : Request.QueryString["DateEnd"].ToDateTime(); ;
string LineNO = Request.QueryString["txtlineNo"] ?? "";
MovementlineInfo moveinfo = imovementline.GetMovementline(LineNO);
moveinfo=moveinfo ?? new MovementlineInfo();
SearchLinesManage linesmanegesearch = new SearchLinesManage
{
SearchType = SearchTypeEnum.Total,
LinedateStart = DateStart,
LinedateEnd = DateEnd,
MoveID = moveinfo.MovementlineID
};
IList<LinesManageInfo> linesManageList = ilinesmanage.SearchLinesManage(linesmanegesearch);
//查看线路计划
//linesManageList=linesManageList.Where(s=>s.LineStatus==LineStatusEnum)
SearchMovecarDetail movedetailsearch = new SearchMovecarDetail
{
SearchType = SearchTypeEnum.Total,
};
IList<MovecarDetailInfo> movecardertailList = imovecardetail.SearchMovecarDetail(movedetailsearch);
movecardertailList = movecardertailList.Where(m => linesManageList.Select(s => s.LinemanageID).Contains(m.PristineCarriercarID)).ToList();
//ViewData["MoveList"] = movecardertailList;
StringBuilder Html = new StringBuilder();
Html.Append("<table><tr>");
Html.Append("<th width='100'>线路日期</th>");
Html.Append("<th width='100'>线路号</th>");;
Html.Append("<th width='100'>路单数</th>");
Html.Append("<th width='100'>计划车次</th>");
Html.Append("<th width='100'>实际车次</th>");
Html.Append("<th width='160'>无GPS信号车辆</th>");
Html.Append("<th width='160'>线路状态</th>");
Html.Append("<th width='160'>最佳路线</th></tr>");
foreach (var item in linesManageList)
{
Html.Append("<tr>");
Html.Append("<td>" + item.LineDate.ToString("yyyy-MM-dd") + "</td>");
Html.Append("<td>" + (BManage.BmEngine.GetProvider<BManage.IMovementline>().GetMovementline(item.LineID) == null ? "" : BManage.BmEngine.GetProvider<BManage.IMovementline>().GetMovementline(item.LineID).MovementlineNo) + "</td>");
Html.Append("<td>" + item.ReceiptNums + "</td>");
Html.Append("<td>" + item.LineTrainnum + "</td>");
Html.Append("<td>" + (movecardertailList.Where(s => s.PristineCarriercarID == item.LinemanageID).ToList().Count) + "</td>");
Html.Append("<td>" + (movecardertailList.Where(s => s.PristineGps == 2).ToList().Count) + "</td>");
Html.Append("<td>" + (Sundear.Base.AttributesHelper.GetEnumDescription<BManage.Enumeration.LineStatusEnum>(item.LineStatus)) + "</td>");
Html.Append("<td>" + item.Ordernos + "</td>");
Html.Append("</tr>");
}Html.Append("</table>");
string PathName = DateTime.Now.ToString("yyyyMMddHHmmss");
ExcelCreate.ExcelCreateXml(Html, PathName);
ExcelCreate.CreateExcelByXml(Html, Server.MapPath("../Upload/" + PathName + ".xls"));
RoleInfo roleinfo = PermissionEngine.GetProvider<IRoleAction>(1).GetRoleDictionary().Values.ToList().Where(s => s.RoleName == "运作跟踪日报收件人").FirstOrDefault();
IList<UserInfo> userlist = iuseraccount.GetUserList(1);
userlist = userlist.Where(w => w.ContainsRole(roleinfo.RoleID)).ToList();
List<string> Emailaddress = userlist.Select(s => s.Profile.Email).ToList();
string Path = Server.MapPath("../Upload/" + PathName + ".xls");
sendEmail.SendEmail(Emailaddress, DateTime.Now.ToString("yyyyMMdd") + "运作跟踪日报", Html.ToString(), Path);
return new EmptyResult();
}