C# 添加到asp excel提取的日期选择条件
我有非常基本的asp.net知识,因为我边学边用(根本没有培训)。我需要将日期范围标准(从、到)添加到当前工作数据提取中。目前,用户只需单击菜单栏中的一个选项,所需的数据就会导出到Excel中。这段代码工作得很好。我们现在需要在导出运行之前添加选择条件。我一直试图这样做,但没有成功。我已经附上了直接出口的代码。非常感谢您的帮助 网站地图C# 添加到asp excel提取的日期选择条件,c#,asp.net,excel,export-to-excel,C#,Asp.net,Excel,Export To Excel,我有非常基本的asp.net知识,因为我边学边用(根本没有培训)。我需要将日期范围标准(从、到)添加到当前工作数据提取中。目前,用户只需单击菜单栏中的一个选项,所需的数据就会导出到Excel中。这段代码工作得很好。我们现在需要在导出运行之前添加选择条件。我一直试图这样做,但没有成功。我已经附上了直接出口的代码。非常感谢您的帮助 网站地图 <siteMapNode url="~/Print_Rep.aspx?SO=&RT=RepDetailedReport"
<siteMapNode url="~/Print_Rep.aspx?SO=&RT=RepDetailedReport" title="Rep" description="Export Rep Information to Excel" />
aspx页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Print_Rep.aspx.cs" Inherits="TEST.Print_Rep" %>
<!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" >
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<head runat="server">
<title>Export Rep Information</title>
</head>
<body style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; WIDTH: 0px; PADDING-TOP: 0px; POSITION: absolute; TOP: 0px; HEIGHT: 0px" bgcolor="whitesmoke">
<form id="PrintRep" method="post" runat="server">
<table cellspacing="0" cellpadding="0" bgcolor="whitesmoke" border="0">
<tr>
<td width="35%" colspan="10"><a style="FONT-WEIGHT: bold; COLOR: #808080; FONT-FAMILY: Arial; FONT-VARIANT: small-caps">
<%= System.Configuration.ConfigurationManager.AppSettings["ApplicationName"] %></a>
<br/>
<asp:label id="lblRep" runat="server" Font-Size="Small" Font-Names="Arial Black" ForeColor="#C00000">Rep</asp:label>
</td>
</tr>
<tr>
<td style="font-weight: 700" colspan="10">
<asp:Label ID="lblDateTime" runat="server" Font-Names="Arial Black" Font-Size="Small" ForeColor="#C00000" Text="TheDate" Width="162px"></asp:Label>
</td>
</tr>
</table>
<table style="FONT-SIZE: small; FONT-FAMILY: Times New Roman" cellpadding="5">
<tr>
<td colspan="4">
<asp:table id="tblActions" runat="server" Visible="False" BorderColor="Black" Height="10px" CellPadding="1" BorderStyle="Solid" BorderWidth="1px" CellSpacing="0" Width="448px">
</asp:table>
</td>
</tr>
</table>
<br />
<asp:Button ID="btnPrintRep" runat="server" BackColor="#969696" BorderColor="#ffffff"
Font-Bold="True" Font-Underline="True" ForeColor="White" OnClick="btnPrintRep_Click"
Text="Print Rep" Visible="False" Width="161px" />
</form>
</body>
</html>
出口代表信息
代表
aspx.cs页面
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.IO;
namespace Test
{
public partial class Print_Rep : System.Web.UI.Page
{
#region Object Definitions
private const string strAll = "All";
private const string strAllValue = "-1";
private const string strGroup = "_SkillFilter_";
public dal dataSource = new dal();
public string RepType = "";
#endregion Object Definitions
#region Page Load
protected void Page_Load(object sender, System.EventArgs e)
{
if (SessionInfo.UserID.ToString() == string.Empty)
{
Response.Redirect("TimeOut.aspx");
}
//if not authorised to enter then send to Access_Denied Page
if (!Security.ValidUser())
{
Response.Redirect("Access_Denied.aspx");
}
if (SessionInfo.SortOrder == "")
SessionInfo.SortOrder = "Course_ID";
if (Request.QueryString.Count > 0)
{
if (Request.QueryString["SO"].ToString() != "")
SessionInfo.SortOrder = Request.QueryString["SO"].ToString();
}
if (!IsPostBack)
{
if (sender.ToString().ToLower() == "asp.print_rep_aspx")
{
if (Request.QueryString.Count > 0)
{
RepType = Request.QueryString["RT"].ToString();
}
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel ";
Response.AddHeader("content-Disposition", "attachment;filename=" +
RepType + ".xls");
Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
Response.Charset = "UTF-8";
CreateTable("export");
DateTime ReportDateTime = DateTime.Now;
lblDateTime.Text = ReportDateTime.ToString("dddd, dd MMM yyyy HH:mm:ss");
lblRep.Text = "Rep";
}
else
{
this.MaintainScrollPositionOnPostBack = true;
}
}
// *** Set up Drop Down list boxes for filtering in table
string controlName = Request.Params.Get("__EVENTTARGET");
}
#endregion Page Load
#region Define DDLs
public string FixNull(Object r)
{
string tempid = "";
if (Convert.IsDBNull(r))
tempid = "NULL";
else
tempid = r.ToString();
return tempid;
}
#endregion Define DDLs
#region Create Table
public void CreateTable(string OutputType)
{
//Set up the table to contain the table definition
DataTable dtTableDefinition = new DataTable();
dtTableDefinition = dataSource.GetCourseTableDefinition();
//Define a date field for use with dates
DateTime convertedDate = new DateTime();
//Empty the current table and make it visible
tblActions.Rows.Clear();
tblActions.Visible = true;
// *** Set up the table
//Add a row to hold the labels
TableRow tRow1 = new TableRow();
tblActions.Rows.Add(tRow1);
tRow1.BackColor = Color.FromArgb(150, 150, 150);
tRow1.ForeColor = Color.White;
tRow1.Font.Bold = true;
tRow1.HorizontalAlign = HorizontalAlign.Center;
tRow1.Font.Name = "Arial";
tRow1.Font.Size = FontUnit.XSmall;
tRow1.BorderColor = Color.Black;
//add the cell information for rows 1 and 2
foreach (DataRow defrow in dtTableDefinition.Rows)
{
if (defrow["Rep"].ToString() == "Y" )//&& defrow["DetailedReport"].ToString() == "Y")
{
//Define cell for row 1
TableCell tCellControls1 = new TableCell();
tCellControls1.BorderColor = Color.Black;
tCellControls1.BorderWidth = 1;
tCellControls1.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
//define cell for row 2
TableCell tCellControls2 = new TableCell();
tCellControls2.BorderColor = Color.Black;
tCellControls2.BorderWidth = 1;
tCellControls1.Text = defrow["Column_Label"].ToString();
//add the cells to the rows
tRow1.Cells.Add(tCellControls1);
}
}
//Set up the table to contain the data
DataTable dtRecommendTable = new DataTable();
dtRecommendTable = dataSource.GetInitialRepTable();
foreach (DataRow datarow in dtRecommendTable.Rows)
{
//start setting up the data rows
TableRow tRow = new TableRow();
tRow.Font.Name = "Arial";
tRow.Font.Size = FontUnit.XSmall;
tRow.CssClass = "dtrow";
tblActions.Rows.Add(tRow);
TableCell tCellRR = new TableCell();
tCellRR.BorderColor = Color.Black;
tCellRR.BorderWidth = 1;
tCellRR.VerticalAlign = VerticalAlign.Top;
foreach (DataRow defrow in dtTableDefinition.Rows)
{
if (defrow["Rep"].ToString() == "Y") //&& defrow["DetailedReport"].ToString() == "Y")
{
TableCell tCell = new TableCell();
tCell.BorderColor = Color.Black;
tCell.BorderWidth = 1;
tCell.BackColor = Color.White;
if (Convert.IsDBNull(datarow[defrow["Column_Name"].ToString()]))
{
tCell.Text = " ";
}
else
{
tCell.Wrap = true;
tCell.VerticalAlign = VerticalAlign.Top;
switch (defrow["Data_Type"].ToString())
{
case "image":
System.Web.UI.WebControls.Image imgPS = new System.Web.UI.WebControls.Image();
imgPS.ImageUrl = datarow[defrow["Column_Name"].ToString()].ToString();
imgPS.Height = 15;
imgPS.Width = 15;
tCell.Controls.Add(imgPS);
tCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
break;
case "date":
convertedDate = Convert.ToDateTime(datarow[defrow["Column_Name"].ToString()].ToString());
tCell.Text = convertedDate.ToString("d MMM yyyy");
break;
case "comment":
tCell.Text = datarow[defrow["Column_Name"].ToString()].ToString();
tCell.Width = 300;
break;
case "longtext":
tCell.Text = RemoveHtmlTag(datarow[defrow["Column_Name"].ToString()].ToString());
tCell.Width = 500;
break;
case "shorttext":
tCell.Text = datarow[defrow["Column_Name"].ToString()].ToString();
tCell.Width = 250;
break;
default:
tCell.Text = datarow[defrow["Column_Name"].ToString()].ToString();
break;
}
}
tRow.Cells.Add(tCell);
}
}
}
}
protected string RemoveHtmlTag(string strHTML)
{
StringWriter myWriter = new StringWriter();
// Decode the encoded string.
HttpUtility.HtmlDecode(strHTML, myWriter);
//Perform necessary REGEX replacement to remove unsupported HTML tags
//Supported HTML tags: SPAN, FONT, STRONG, EM, CENTER, DIV, P
//remove Html Tags
string strResult = myWriter.ToString();
string[] removeTagArray = new string[] { "span", "font", "strong", "em", "center", "div", "ol", "ul", "a" };
foreach (string tag in removeTagArray)
{
string regExp = string.Format("</?{0}([^>]*)>", tag);
strResult = Regex.Replace(strResult, regExp, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
string[] newLineTagArray = new string[] { "br", "p" };
foreach (string tag in newLineTagArray)
{
string regExp = string.Format("</?{0}([^>]*)>", tag);
strResult = Regex.Replace(strResult, regExp, "\r\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
string[] itemTagArray = new string[] { "li" };
foreach (string tag in itemTagArray)
{
string regExp = string.Format("<{0}([^>]*)>", tag);
strResult = Regex.Replace(strResult, regExp, " .", RegexOptions.IgnoreCase | RegexOptions.Compiled);
regExp = string.Format("</{0}([^>]*)>", tag);
strResult = Regex.Replace(strResult, regExp, "\r\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
string strResult2 = strResult;
//Replace 2 or more blanks with one blank
strResult2 = Regex.Replace(strResult2, "[ ]{2,}", " ");
//Replace 3 or more empty lines with 2 empty lines
strResult2 = Regex.Replace(strResult2, "([ ]*[\r\n][ ]*)+", Environment.NewLine, RegexOptions.Multiline);
if (strResult2.StartsWith(Environment.NewLine))
strResult2 = strResult2.Remove(0, Environment.NewLine.Length);
if (strResult2.EndsWith(Environment.NewLine))
strResult2 = strResult2.Remove(strResult2.Length - Environment.NewLine.Length);
return strResult2;
}
#endregion Create Table
#region Object Clicked
public void btnPrintRep_Click(object sender, System.EventArgs e)
{
Response.Redirect("Print_Rep.aspx?SO=&RT=DetailedReport", false);
}
#endregion Object Clicked
}
}
使用系统;
使用系统数据;
使用系统图;
使用System.Web;
使用System.Web.UI.WebControl;
使用System.Text.RegularExpressions;
使用System.IO;
名称空间测试
{
公共部分类打印_Rep:System.Web.UI.Page
{
#区域对象定义
private const string strAll=“All”;
私有常量字符串strAllValue=“-1”;
private const string strGroup=“\u SkillFilter\u”;
公共dal数据源=新dal();
公共字符串RepType=“”;
#端域对象定义
#区域页面加载
受保护的无效页面加载(对象发送方,System.EventArgs e)
{
if(SessionInfo.UserID.ToString()==string.Empty)
{
重定向(“TimeOut.aspx”);
}
//如果未授权进入,则发送至“访问被拒绝”页面
如果(!Security.ValidUser())
{
重定向(“访问被拒绝.aspx”);
}
if(SessionInfo.SortOrder==“”)
SessionInfo.SortOrder=“课程ID”;
如果(Request.QueryString.Count>0)
{
if(Request.QueryString[“SO”].ToString()!=“”)
SessionInfo.SortOrder=Request.QueryString[“SO”].ToString();
}
如果(!IsPostBack)
{
if(sender.ToString().ToLower()=“asp.print\u rep\u aspx”)
{
如果(Request.QueryString.Count>0)
{
RepType=Request.QueryString[“RT”].ToString();
}
Response.Buffer=true;
Response.ContentType=“application/vnd.ms-excel”;
AddHeader(“内容处置”、“附件;文件名=”+
RepType+“.xls”);
回答。写(@“);
响应。Charset=“UTF-8”;
CreateTable(“导出”);
DateTime ReportDateTime=DateTime.Now;
lblDateTime.Text=ReportDateTime.ToString(“dddd,dd-MMM-yyyy-HH:mm:ss”);
lblRep.Text=“Rep”;
}
其他的
{
this.MaintainScrollPositionOnPostBack=true;
}
}
//***设置下拉列表框以便在表中进行筛选
string controlName=Request.Params.Get(“\uu EVENTTARGET”);
}
#endregion页面加载
#区域定义DDL
公共字符串FixNull(对象r)
{
字符串tempid=“”;
if(转换为IsDBNull(r))
tempid=“NULL”;
其他的
tempid=r.ToString();
返回tempid;
}
#端域定义DDL
#区域创建表
公共void CreateTable(字符串输出类型)
{
//设置表以包含表定义
DataTable dtTableDefinition=新DataTable();
dtTableDefinition=dataSource.GetCourseTableDefinition();
//定义与日期一起使用的日期字段
DateTime convertedDate=新的日期时间();
//清空当前表并使其可见
tblActions.Rows.Clear();
tblActions.Visible=true;
//***摆好桌子
//添加一行以保存标签
TableRow tRow1=新的TableRow();
tblActions.Rows.Add(tRow1);
tRow1.BackColor=Color.FromArgb(150150150);
tRow1.ForeColor=颜色。白色;
tRow1.Font.Bold=true;
tRow1.HorizontalAlign=HorizontalAlign.Center;
tRow1.Font.Name=“Arial”;
tRow1.Font.Size=FontUnit.XSmall;
tRow1.BorderColor=颜色.黑色;
//添加第1行和第2行的单元格信息
foreach(dtTableDefinition.Rows中的DataRow defrow)
{
if(defrow[“Rep”].ToString()=“Y”)/&&defrow[“DetailedReport”].ToString()=“Y”)
{
//为第1行定义单元格
TableCell tCellControls1=新的TableCell();
tCellControls1.BorderColor=颜色.黑色;
tCellControls1.BorderWidth=1;
tCellControls1.HorizontalAlign=System.Web.UI.WebControls.HorizontalAlign.Left;
//为第2行定义单元格
TableCell tCellControls2=新的TableCell();
tCellControls2.BorderColor=颜色.黑色;
tCellControls2.BorderWidth=1;
tCellControls1.Text=defrow[“Column_Label”].ToString();
//将单元格添加到行中
tRow1.Cells.Add(