我是C#的新手,我在一个程序中工作,而不是从SQL Server数据库中下载包含数据的文件数据,该查询总是将相同的旧的不存在的数据带入文件中。
这是BORA.asmx.cs服务:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Text;
using LST.Enums;
using LST.Extensions;
using LST.Global;
using LST.Methods;
using Newtonsoft.Json;
using System.Diagnostics.Tracing;
namespace BORA
{
[WebMethod(EnableSession = true)]
public byte[] generateReport()
{
var vUser = Session["rep-users"];
var vCat = Session["rep-categories"];
var vStatus = Session["rep-status"];
var vAreas = Session["rep-areas"];
string strWhere = "";
if (vUser.fwEmpty() != "")
strWhere += (strWhere == "" ? "" : " AND ") + "P.Owner IN (" + vUser.ToString().fwSqlFilterIn() + ")";
if (vCat.fwEmpty() != "")
strWhere += (strWhere == "" ? "" : " AND ") + "Category IN (" + vCat.ToString().fwSqlFilterIn() + ")";
if (vStatus.fwEmpty() != "")
strWhere += (strWhere == "" ? "" : " AND ") + "P.Status IN (" + vStatus.ToString().fwSqlFilterIn() + ")";
if (vAreas.fwEmpty() != "")
strWhere += (strWhere == "" ? "" : " AND ") + "Area IN (" + vAreas.ToString().fwSqlFilterIn() + ")";
if (strWhere != "") strWhere = " WHERE " + strWhere;
System.Diagnostics.Debug.WriteLine("strWhere = " + strWhere);
LST.Methods.clsMethodsWeb fw = new clsMethodsWeb();
string strPathOrigin = fw.appGetPath + "excel\\template.xlsx";
string strPathDest = fw.appGetPath + "excel\\{0}.xlsx".fwFormat(Session["User"].ToString().ToLower().Replace(".", ""));
string strTable = "";
string strPID = "";
strPID = "WHERE PID IN (" +
@"SELECT P.PID
FROM tbl_Project_Tasks P
LEFT OUTER JOIN tbl_Project_Details D
ON P.PID = D.PID {0}"
.fwFormat(strWhere)
.fwSqlFillDataTable()
.fwDistinctValues("PID")
.fwSqlFilterIn() + ")";
if (strPID.fwEmpty() == "") return null;
fw.fileCopy(strPathOrigin, strPathDest);
DataTable dt = @"SELECT *
FROM tbl_Project_Tasks {0}"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Tasks";
strTable = "tblTasks";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt },
strPathDest, strTable: strTable, blnSave: true);
// ** Tasks
dt = @"SELECT *
FROM tbl_Project_Details D
LEFT OUTER JOIN tbl_Project_SavingType S
ON D.PID = S.PID
{0}"
.fwFormat(strPID.Replace(" PID ", " D.PID "))
.fwSqlFillDataTable();
dt.TableName = "Projects";
strTable = "tblProjects";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt }, strPathDest, strTable: strTable, blnSave: true);
// ** Projects
dt = @"SELECT REPLACE(REPLACE(SavingType, '(', ''), ')', '') Project, SUM(Saving) Projected, SUM(SavingFinal) Confirmed
FROM tbl_Project_SavingType
{0}
GROUP BY SavingType"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Project consolidate";
strTable = "tblProjectConsolidate";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt }, strPathDest, strTable: strTable, blnSave: true);
// ** Project consolidate
// ** tblProjectConsolidate
dt = @"SELECT Status, Count(Status) Count
FROM tbl_Project_Tasks
{0}
AND ISNULL(Status, '') <> ''
GROUP BY Status"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Task status";
strTable = "tblTaskStatus";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt },
strPathDest, strTable: strTable, blnSave: true);
// ** Task status
// ** tblTaskStatus
dt = @"Select Status, COUNT(Status) Count
FROM tbl_Project_Details
{0}
GROUP BY Status"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Project status";
strTable = "tblProjectStatus";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt },
strPathDest, strTable: strTable, blnSave: true);
// ** Project status
// ** tblProjectStatus
dt = @"Select Category, COUNT(Category) Count
FROM tbl_Project_Details
{0}
GROUP BY Category
ORDER BY Count"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Project categories";
strTable = "tblProjectCategories";
LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt },
strPathDest, strTable: strTable, blnSave: true);
// ** Poject categories
// ** tblProjectCategories
dt = @"Select Area, Count(Area) Count
FROM tbl_Project_Details
{0}
GROUP BY Area
ORDER BY Count"
.fwFormat(strPID)
.fwSqlFillDataTable();
dt.TableName = "Project areas";
strTable = "tblProjectAreas";
return LST.EPP.stcEpp.exportToByteArray(new DataTable[] { dt },
strPathDest, strTable: strTable);
// ** Project areas
// ** tblProjectAreas
}
[WebMethod(EnableSession = true)]
public string getReportOptions()
{
string dtArea = ("SELECT DISTINCT AREA FROM tbl_Maint_Area").fwSqlFillDataTable().fwDistinctValues();
string dtCategory = ("SELECT DISTINCT CATEGORY FROM tbl_Maint_Category").fwSqlFillDataTable().fwDistinctValues();
string dtStatus = ("SELECT DISTINCT STATUS FROM tbl_Maint_Status").fwSqlFillDataTable().fwDistinctValues();
string dtUsers = ("SELECT DISTINCT USERNAME FROM tbl_Maint_User").fwSqlFillDataTable().fwDistinctValues();
return JsonConvert.SerializeObject(new
{
Areas = dtArea,
Categories = dtCategory,
Status = dtStatus,
Users = dtUsers
});
}
[WebMethod(EnableSession = true)]
public string fillParams(string users, string categories, string status, string areas)
{
Session["rep-users"] = users;
Session["rep-categories"] = categories;
Session["rep-status"] = status;
Session["rep-areas"] = areas;
return "";
}
}
}
这是导出的.aspx.cs:export.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BORA
{
public partial class export : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
byte[] arr = null;
try
{
clsBORA report = new clsBORA();
arr = report.generateReport();
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=\"BORA Scorecard.xlsx\"");
Response.SetCookie(new HttpCookie("fileDownload", "true"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(arr);
//Response.End();
}
catch { /* DO NOTHING */ }
finally
{
if (arr != null) Array.Clear(arr, 0, arr.Length);
GC.Collect();
}
}
}
}
aspx文件名为:export.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="export.aspx.cs" Inherits="BORA.export" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
查询是正确的,html部分还可以,可以正常工作,但是它仍然下载错误的数据,此外,我该如何打印字节数组?
我还有什么需要补充的?
此外,如何打印“ .fwswlfilldata”。
再次,我是编程的新手,喜欢它,想学习,但有时我需要这些方面的帮助。
非常感谢您