这是我的代码,用于从单个excel工作表中的多个表下载数据。如何在此代码中添加两个日期? 该代码非常适合下载,但我需要下载两个日期。
控制器 这是两个表,使用datatable从数据库中选择所有数据
Table 1
private DataTable getTable1()
{
string constr = ConfigurationManager.ConnectionStrings["db_Demo"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 ORDER BY ID"))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
}
}
}
Table 1
private DataTable getTable2()
{
string constr = ConfigurationManager.ConnectionStrings["db_Demo"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table2 ORDER BY ID"))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
}
}
}
public DataSet getDataSetExportToExcel()
{
DataSet ds = new DataSet();
DataTable dtTable1 = new DataTable("Table1");
dtTable1 = getTable1();
DataTable dtTable2 = new DataTable("Table2");
dtTable2 = getTable2();
ds.Tables.Add(dtTable1);
ds.Tables.Add(dtTable2);
return ds;
}
public ActionResult btn_Export_Click()
{
DataSet ds = getDataSetExportToExcel();
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(ds);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= TablesData.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
return View();
}
查看 这些是日期
<div>
To: <input type="date" value="startdate" />
From: <input type="date" value="enddate" />
</div>
<div>
<a href="@Url.Action("btn_Export_Click", "Data")" class="btn btn-success"> Export Data</a>
</div>
答案 0 :(得分:1)
假设表中的日期字段称为“ RefDate”,则可以尝试执行以下操作:
private static DataTable GetTable(string table, DateTime startDate, DateTime endDate)
{
string constr = ConfigurationManager.ConnectionStrings["db_Demo"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand($"SELECT * FROM {table} WHERE RefDate BETWEEN @startDate AND @endDate ORDER BY ID"))
{
var startDateParameter = new SqlParameter("@startDate", SqlDbType.DateTime);
startDateParameter.Value = startDate;
var endDateParemeter = new SqlParameter("@endDate", SqlDbType.DateTime);
endDateParemeter.Value = endDate;
cmd.Parameters.Add(startDateParameter);
cmd.Parameters.Add(endDateParemeter );
using (SqlDataAdapter da = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
}
}
}
private static DataSet GetDataSetExportToExcel(DateTime startDate, DateTime endDate)
{
var dtTable1 = GetTable("Table1", startDate, endDate);
var dtTable2 = GetTable("Table2", startDate, endDate);
var ds = new DataSet();
ds.Tables.Add(dtTable1);
ds.Tables.Add(dtTable2);
return ds;
}
[HttpGet]
public ActionResult Export()
{
return View();
}
[HttpPost]
public ActionResult Export(DateTime startDate, DateTime endDate)
{
DataSet ds = GetDataSetExportToExcel(startDate, endDate);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(ds);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= TablesData.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
return View();
}
您需要将视图重命名为“导出”,并对其进行调整以通过以下方式发送数据:
@using (Html.BeginForm("Export", "Form", FormMethod.Post))
{
<div>
<label for="startDate">From:</label>
@Html.TextBox("startDate", "{0:dd/MM/yyyy}",
new { @class = "form-control date", type = "date" })
<label for="endDate">To:</label>
@Html.TextBox("endDate", "{0:dd/MM/yyyy}",
new { @class = "form-control date", type = "date" })
</div>
<input type="submit" class="btn btn-success" value="Export Data" />
}