引用
using Microsoft.Office.Interop.Excel;
using System.Reflection;//反射命名空间
using System.IO;
protected void Button1_Click(object sender, EventArgs e)
{
//项目引入Microsoft.office.Excel组件
//导出二大部分
//第一部分,生成EXCEL文件
//对象1 应用程序对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//对象2 工作薄对象
Workbook book = excel.Workbooks.Add(Missing.Value);
//对象3 工作表对象(工作薄中的第一个工作表)
Worksheet worksheet = (Worksheet)book.Worksheets[1];
////////////////////////////////////////////////////////////////////////////////////////
worksheet.Name = "N-Tier";
worksheet.Cells[5, 6] = "欢迎学习 N-Tier";//注意定义单元格中的方法
worksheet.get_Range("A2", Missing.Value).Value2 = "星期";
worksheet.get_Range("A2", Missing.Value).Font.Name = "黑体";
worksheet.get_Range("A2", Missing.Value).Font.Size = 14;
worksheet.get_Range("A2", Missing.Value).RowHeight = 28;
worksheet.get_Range("A1", "Y1").MergeCells = true;//合并单元格
worksheet.get_Range("A1", "Y1").Value2 = "教室机房周占用表";
worksheet.get_Range("A1", "Y1").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
worksheet.get_Range("A1", "Y1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignBottom;
worksheet.get_Range("A1", "Y1").Font.Bold = true;
worksheet.get_Range("A1", "Y1").Font.Name = "黑体";
worksheet.get_Range("A1", "Y1").Font.Size = 20;
worksheet.get_Range("A1", "Y1").RowHeight = 63;
///////////////////////////////////////////////////////////////////////////
//保存EXCEL部分
excel.Visible = false;//应用程序可见
//Excel文件名
string excelName = Server.MapPath("~/temp/")+DateTime.Now.ToString("yyyyMMdd-hhmmss")+".xls";
excel.ActiveWorkbook.SaveAs(excelName, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);
//关闭工作薄
excel.ActiveWorkbook.Close(true, Missing.Value, Missing.Value);
//退出应用程序
excel.Quit();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//第二部分,把生成EXCEL文件下载
DownLoadFile(excelName, "学生信息表.xls", this);
/////////////////////////////////////////////////////////////////
}
#region DownLoadFile
/// <summary>
/// 下载所有类型文件通用
/// </summary>
/// <param name="fileName">文件的虚拟路径</param>
/// <param name="downFileName">新名称</param>
/// <param name="page">下载的页面对象</param>
public void DownLoadFile(string fileName, string downFileName, Page page)
{
//清除缓冲区中的所有HTML输出
page.Response.Clear();
page.Response.Buffer = true;
//设置输出流的HTTP字符集
page.Response.Charset = "utf-8";
//attachment参数表示作为附件下载
string attachment = "attachment; filename=" + HttpUtility.UrlEncode(downFileName, System.Text.Encoding.UTF8);
page.Response.AddHeader("content-disposition", attachment);
//设置输出流的HttpMIMe类型(以流形式输出)
page.Response.ContentType = "application/octent-stream";
page.Response.WriteFile(fileName);
page.Response.End();
}
#endregion
protected void Button2_Click(object sender, EventArgs e)
{
//项目引入Microsoft.office.Excel组件
//导出二大部分
//第一部分,生成EXCEL文件
//对象1 应用程序对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//对象2 工作薄对象
Workbook book = excel.Workbooks.Add(Missing.Value);
//对象3 工作表对象(工作薄中的第一个工作表)
Worksheet worksheet = (Worksheet)book.Worksheets[1];
////////////////////////////////////////////////////////////////////////////////////////
DataClassesDataContext datacontext = new DataClassesDataContext();
var stusList = datacontext.Student.ToList();
for (int i = 0; i <stusList.Count; i++)
{
worksheet.Cells[i + 1, 1] = stusList[i].Name;
worksheet.Cells[i + 1, 2] = stusList[i].Sex;
}
///////////////////////////////////////////////////////////////////////////
//保存EXCEL部分
excel.Visible = false;//应用程序可见
//Excel文件名
string excelName = Server.MapPath("~/temp/") + DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".xls";
excel.ActiveWorkbook.SaveAs(excelName, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);
//关闭工作薄
excel.ActiveWorkbook.Close(true, Missing.Value, Missing.Value);
//退出应用程序
excel.Quit();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//第二部分,把生成EXCEL文件下载
DownLoadFile(excelName, "ss学生信息表.xls", this);
}
原文地址:http://www.cnblogs.com/panjuan/p/3811513.html