BLL
/// <summary>
/// 匯出/// </summary>
/// <param name="p_FileName"></param>
/// <returns></returns>
public bool ExportConferenceRoom(string p_FileName)
{
bool flag = false;
List<BaseInfo> p_ListConferenceRoom = m_ConferenceRoom.QueryALLConferenceRoom();
flag = ExportExcel(p_ListConferenceRoom, p_FileName);
return flag;
}
#endregion
/// <summary>
///導出數據
/// </summary>
/// <param name="p_ListConferenceRoom"></param>
/// <param name="p_fileName"></param>
/// <returns></returns>
private bool ExportExcel(List<BaseInfo> p_ListConferenceRoom, string p_fileName)
{
bool flag = false;
ApplicationClass excel = new ApplicationClass();
excel.Application.DisplayAlerts = false;
Workbooks workBooks = excel.Workbooks;
Workbook workBook = workBooks.Add(true);
Worksheet sheet = (Worksheet)workBook.Worksheets[1];
Missing missing = Missing.Value;
//初始化表頭頭
int colIndex = 2;
sheet.Cells[1, 1] = "會議室名稱";
sheet.Cells[1, 2] = "會議室描述";
//設置表頭居中
sheet.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
sheet.get_Range(excel.Cells[1, 2], excel.Cells[1, 2]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
//設置表頭字體加粗
sheet.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
sheet.get_Range(excel.Cells[1, 2], excel.Cells[1, 2]).Font.Bold = true;
//綁定數據
int rowSum = 1;
for (int i = 0; i < p_ListConferenceRoom.Count; i++)
{
rowSum++;
BaseInfo p_ConferenceRoom = new BaseInfo();
p_ConferenceRoom = p_ListConferenceRoom[i];
sheet.Cells[i + 2, 1] = p_ConferenceRoom.BaseInfoName;
sheet.Cells[i + 2, 2] = p_ConferenceRoom.BaseInfoDesc;
}
//设置表格为最适应宽度
sheet.get_Range(excel.Cells[1, 1], excel.Cells[rowSum, colIndex]).Columns.AutoFit();
//绘制边框
sheet.get_Range(excel.Cells[1, 1], excel.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
//设置左边线加粗
sheet.get_Range(excel.Cells[1, 1], excel.Cells[rowSum, 1]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
//设置上边线加粗
sheet.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;
//设置右边线加粗
sheet.get_Range(excel.Cells[1, colIndex], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
//设置下边线加粗
sheet.get_Range(excel.Cells[rowSum, 1], excel.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;
excel.DisplayAlerts = false;
excel.Visible = false;
//保存Excel
try
{
sheet.SaveAs(p_fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBooks.Close();
excel.Quit();
flag = true;
}
catch
{
flag = false;
}
return flag;
}
Controller:
/// <summary>
/// 下載到到Client端。
/// </summary>
/// <param name="filePath"></param>
public void DownLoad(string fileFullPath)
{
string fileName = System.IO.Path.GetFileName(fileFullPath);
string filePath = System.IO.Path.GetFullPath(fileFullPath);
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
try
{
if (System.IO.File.Exists(fileFullPath))
{
Encoding encodeUTF8 = System.Text.Encoding.UTF8;
fileName = HttpUtility.UrlEncode(fileName, encodeUTF8);
Response.Charset = "UTF-8";
Response.HeaderEncoding = encodeUTF8;
Response.WriteFile(filePath);// 把文件流发送到客户端
Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载,设置输出文件类型为excel文件。
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
//Response.BinaryWrite(ReadByte(filePath));
Response.Flush();
System.IO.File.Delete(filePath);
Response.End();
}
else
{
Response.Write("您訪問的文件不存在");
Response.Flush();
Response.End();// 停止頁面的执行
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 匯出
/// </summary>
public ActionResult ExportConferenceRoom()
{
try
{
string strFileName = Server.MapPath("../") + "UploadFiles\\tmp.xls";
bool isSuccessed = m_BLConferenceRoom.ExportConferenceRoom(strFileName);
if (isSuccessed)
{
return new JsonResult { Data = new { success = true, result = "匯出成功", path = strFileName } };
}
else
{
return new JsonResult { Data = new { success = false, result = "匯出失敗", path = strFileName } };
}
}
catch (Exception ex)
{
return new JsonResult { Data = new { success = false, result = ex.Message } };
}
}
JS:
//Export
function ExportConferenceRoom() {
$('#picBodyArea5').show();
$('#freshareaconferenceroom').hide();
$.post('<%=Url.Action("ExportConferenceRoom","BaseData") %>?random=' + Math.random(),
function(data) {
if (data.success) {
window.location.href = '<%=Url.Action("DownLoad","BaseData") %>?random=' + Math.random() + '&fileFullPath=' + data.path;
$('#picBodyArea5').hide();
$('#freshareaconferenceroom').show();
}
else {
alert(data.result);
}
}, "json");
}