using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using JZZC863Dal;
using JZZC863Dal.Model;
using Excel = Microsoft.Office.Interop.Excel; //添加引用
using System.Diagnostics.Eventing.Reader;
using JZZC863Bll;
using JZZC863.Models;
using System.IO;
namespace JZZC863.Controllers
{
public class ZBGL3Controller : Controller
{
/// <summary>
/// excel导出
/// </summary>
/// <returns></returns>
public ActionResult GetInfo()
{
bool success = true;
string msg = "成功";
string userNum = LoginVM.GetUserNumber();
CommonBllHelper.CreateUserDir(userNum);
Excel.Application excel1 = new Excel.Application();
excel1.DisplayAlerts = false;
Excel.Workbook workbook1 = excel1.Workbooks.Add(Type.Missing);
excel1.Visible = false;
Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook1.Worksheets["sheet1"];
string tn = "CZSZC";
List<DescriptionDao> list = JZZC863DalHelper.GetDescriptionDaoList(tn);
///获取表头
for (int i = 0; i < list.Count; i++)//行
{
worksheet1.Cells[1, i + 1] = list[i].ZWHY; //Excel里从第1行,第1列计算
}
//获取数据
string cols = string.Join(",", list.Select(p => p.ZDM));
string where = "";
List<List<string>> listCZSZC = JZZC863DalHelper.GetTableDataList(tn, cols, where);
for (int i = 0; i < listCZSZC.Count; i++)
{
for (int j = 0; j < listCZSZC[i].Count; j++)
{
worksheet1.Cells[i + 2, j + 1] = listCZSZC[i][j]; //Excel里从第1行,第1列计算
}
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string filePath = System.Web.HttpContext.Current.Server.MapPath("~/kehu/" + userNum + "/excel/") + fileName;
workbook1.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel1.Workbooks.Close();
excel1.Quit();
//关闭该文件
if (System.IO.File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
if (Request.UserAgent != null)
{
string userAgent = Request.UserAgent.ToUpper();
if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
}
}
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
System.IO.File.Delete(filePath);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
else
{
Response.Write("文件未找到,可能已经被删除");
Response.Flush();
Response.End();
}
var rlt = new { success = success, msg = msg };
return Json(rlt, JsonRequestBehavior.AllowGet);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using JZZC863Dal;
using JZZC863Dal.Model;
using Excel = Microsoft.Office.Interop.Excel; //添加引用
using System.Diagnostics.Eventing.Reader;
using JZZC863Bll;
using JZZC863.Models;
using System.IO;
namespace JZZC863.Controllers
{
public class ZBGL3Controller : Controller
{
/// <summary>
/// excel导出
/// </summary>
/// <returns></returns>
public ActionResult GetInfo()
{
bool success = true;
string msg = "成功";
string userNum = LoginVM.GetUserNumber();
CommonBllHelper.CreateUserDir(userNum);
Excel.Application excel1 = new Excel.Application();
excel1.DisplayAlerts = false;
Excel.Workbook workbook1 = excel1.Workbooks.Add(Type.Missing);
excel1.Visible = false;
Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook1.Worksheets["sheet1"];
string tn = "CZSZC";
List<DescriptionDao> list = JZZC863DalHelper.GetDescriptionDaoList(tn);
///获取表头
for (int i = 0; i < list.Count; i++)//行
{
worksheet1.Cells[1, i + 1] = list[i].ZWHY; //Excel里从第1行,第1列计算
}
//获取数据
string cols = string.Join(",", list.Select(p => p.ZDM));
string where = "";
List<List<string>> listCZSZC = JZZC863DalHelper.GetTableDataList(tn, cols, where);
for (int i = 0; i < listCZSZC.Count; i++)
{
for (int j = 0; j < listCZSZC[i].Count; j++)
{
worksheet1.Cells[i + 2, j + 1] = listCZSZC[i][j]; //Excel里从第1行,第1列计算
}
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
string filePath = System.Web.HttpContext.Current.Server.MapPath("~/kehu/" + userNum + "/excel/") + fileName;
workbook1.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel1.Workbooks.Close();
excel1.Quit();
//关闭该文件
if (System.IO.File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
if (Request.UserAgent != null)
{
string userAgent = Request.UserAgent.ToUpper();
if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
}
}
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
System.IO.File.Delete(filePath);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
else
{
Response.Write("文件未找到,可能已经被删除");
Response.Flush();
Response.End();
}
var rlt = new { success = success, msg = msg };
return Json(rlt, JsonRequestBehavior.AllowGet);
}
}
}