标签:
页面代码 :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SaleslDailyReport.aspx.cs" Inherits="DataReport.OilDistribution.SaleslDailyReport" %>
<!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 runat="server">
<title>销售日报</title>
<script src="../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="../Scripts/boot.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="mini-toolbar" style="padding: 2px; border-bottom: 0;">
<table style="width: 100%; border: 1px;">
<tr>
<td align="right" height="24" width="100px;">
时间:
</td>
<td align="left">
<input type="text" id="txtCreTime" format="yyyy-MM-dd" class="mini-datepicker" showtime="true" />
</td>
<td align="right">
<a class="mini-button" iconcls="icon-download" onclick="Search()">查询</a>
</td>
<td align="left">
<a class="mini-button" iconcls="icon-download" onclick="down()">下载</a>
</td>
</tr>
</table>
</div>
<table style="width: 100%; border: 1px;">
<tr>
<td>
</td>
<td align="center">
<h3> 销售日报</h3>
</td>
<td align="right" style="width: 200px;"> 单位:万吨 </td>
</tr> </table>
<div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 600px;" showpager="false">
<div property="columns">
<div field="Alias" width="120" headeralign="center" allowsort="true" align="center"> 单位
</div>
<div header="当日销售" headeralign="center">
<div property="columns">
<div field="dxx" width="100" allowsort="true" headeralign="center" align="center"> 小计</div>
<div field="dxq" width="100" allowsort="true" headeralign="center" align="center"> 汽油</div>
<div field="dxc" width="100" allowsort="true" headeralign="center" align="center"> 柴油</div>
<div field="dxm" width="100" allowsort="true" headeralign="center" align="center"> 煤油</div>
</div>
</div>
<div header="月累计" headeralign="center">
<div property="columns">
<div field="ylx" width="100" allowsort="true" headeralign="center" align="center"> 小计</div>
<div field="ylq" width="100" allowsort="true" headeralign="center" align="center"> 汽油</div>
<div field="ylc" width="100" allowsort="true" headeralign="center" align="center"> 柴油</div>
<div field="ylm" width="100" allowsort="true" headeralign="center" align="center"> 煤油</div>
</div>
</div>
<div header="当日库存" headeralign="center">
<div property="columns">
<div field="dkx" width="100" allowsort="true" headeralign="center" align="center"> 小计</div>
<div field="dkq" width="100" allowsort="true" headeralign="center" align="center"> 汽油</div>
<div field="dkc" width="100" allowsort="true" headeralign="center" align="center"> 柴油</div>
<div field="dkm" width="100" allowsort="true" headeralign="center" align="center"> 煤油</div>
</div>
</div>
</div>
</div>
</div>
<iframe id="exportIFrame" style="display: none;"></iframe>
</form>
</body>
</html>
<script type="text/javascript">
mini.parse();
var grid = mini.get("datagrid1");
//下载 导出数据
function down() {
var time = mini.get("txtCreTime").getFormValue();
var url = bootPATH + "../OilDistribution/AjaxReport.ashx?method=ExportExcel&time=" + time;
var exportIFrame = document.getElementById("exportIFrame");
exportIFrame.src = url;
}
</script>
一般处理程序代码
protected override object processQuery(HttpContext context, out bool hasError)
{
hasError = false;
string method = context.Request["method"];
switch (method)
{
case "ExportExcel": //下载(销售日报导出)
return ExportDataToExcel(context, out hasError);
}
}
#region 下载
/// <summary>
/// 下载(导出销售日报)
/// </summary>
/// <param name="context"></param>
/// <param name="hasError"></param>
/// <returns></returns>
private object ExportDataToExcel(HttpContext context, out bool hasError)
{
string result = "";
hasError = false;
try {
ReportState32BLL sbll = new ReportState32BLL();
string time = context.Request["time"];
//获得要导出的数据信息(销售日报)
DataTable data = sbll.GetSaleslDailyTable(time);
if (data != null)
{
string fileName = "销售日报(" + DateTime.Now.ToString("yyyyMMddhhmmss") + ").xlsx";
string[] columnName = { "序号", "单位", "小计", "汽油", "柴油", "煤油", "小计", "汽油", "柴油", "月累计煤油", "小计", "汽油", "柴油", "煤油" };
string tempDir = context.Server.MapPath("~/DownLoadFiles/");
//临时存放目录
string tempXls = string.Format("{0}{1}", tempDir, fileName);
SaveFile(data, fileName, tempDir, columnName);
ExportToExcel(context, tempXls);
result = "导出成功";
}
else
{
result = "没有要导出的数据";
}
}
catch (Exception ex)
{
result = "导出失败!";
}
return result;
}
/// <summary>
/// Excel文件保存到服务器 Excel文件版本:office 2007
/// </summary>
/// <param name="dt">数据表</param>
/// <param name="fileName">服务器端文件名称</param>
/// <param name="filePath">服务器端文件路径</param>
/// <param name="columnName">表头</param>
/// <param name="insertColumnIdnex">插入空列索引</param>
private void SaveFile(System.Data.DataTable dt, string fileName, string filePath, string[] columnName = null)
{
Application app = null;
Workbook wb = null;
Worksheet sheet = null;
try {
string[] field = { "Alias", "dxx", "dxq", "dxc", "dxm", "ylx", "ylq", "ylc", "ylm", "dkx", "dkq", "dkc", "dkm" };
app = new Application();
app.Visible = false;
wb = (Workbook)app.Workbooks.Add(Missing.Value);
sheet = (Worksheet)wb.ActiveSheet;
DataTable result = dt;
if (columnName != null && columnName.Length > 0)
{ for (int i = 0; i < columnName.Length; i++)
{
string a = columnName[i].ToString();
sheet.Cells[1, i + 1].Value2 = columnName[i];
}
}
//内容填充
for (int j = 0; j < result.Rows.Count; j++)
{
sheet.Cells[j + 2, 1].Value2 = j + 1;
for (int k = 0; k < field.Length; k++)
{
string datavalue = "";
datavalue = result.Rows[j][field[k]].ToString();
sheet.Cells[j + 2, k + 2].Value2 = datavalue;
}
}
//设置Sheet整体样式
sheet.Columns.EntireColumn.AutoFit();
sheet.Cells.HorizontalAlignment = XlHAlign.xlHAlignLeft;
sheet.Rows.RowHeight = 18;
//设置标题信息
Range ran;
ran = sheet.Range[sheet.Cells[1, 1], sheet.Cells[1, columnName.Length + 1]];
//取得区域
ran.Font.Bold = FontStyle.Bold;
ran.Rows.RowHeight = 30;
wb.Saved = true;
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
DirectoryInfo dinfo = new DirectoryInfo(filePath);
dinfo.Attributes = FileAttributes.Normal;
string fileInfo = String.Format("{0}{1}", filePath, fileName);
if (File.Exists(fileInfo))
File.Delete(fileInfo);
app.ActiveWorkbook.SaveCopyAs(fileInfo);
}
catch (Exception ex)
{
}
finally
{
wb.Close(null, null, null);
app.Workbooks.Close();
app.Quit();
Marshal.ReleaseComObject((object)app);
Marshal.ReleaseComObject((object)wb);
Marshal.ReleaseComObject((object)sheet);
GC.Collect();
}
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="xlsfile"></param>
private void ExportToExcel(HttpContext context, string xlsfile)
{
System.IO.FileInfo file = new System.IO.FileInfo(xlsfile);
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
context.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
context.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
context.Response.WriteFile(file.FullName);
// 停止页面的执行
context.Response.End();
}
#endregion
标签:
原文地址:http://www.cnblogs.com/lk516924/p/5504488.html