导出Excel (application方法)
来源:互联网 发布:手机淘宝优惠券怎么用? 编辑:程序博客网 时间:2024/05/17 22:03
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
bll bl = new bll();
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
Repeater1.DataSource = bl.GetView();
Repeater1.DataBind();
}
public void OutputExcel(DataView dv, string str)
{
Excel.Range range;
GC.Collect();
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
int rowIndex = 2;
int colIndex = 0;
Excel._Workbook book;
Excel._Worksheet sheet;
book = app.Workbooks.Add(true); //新建一张excel工作簿
sheet = (Excel._Worksheet)book.ActiveSheet;
//取得标题
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
app.Cells[2, colIndex] = col.ColumnName;
sheet.get_Range(app.Cells[2, colIndex], app.Cells[2, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//标题居中显示
}
//取得数据
foreach (DataRowView row in dv)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex++;
range = sheet.get_Range(app.Cells[rowIndex, colIndex], app.Cells[rowIndex, colIndex]);
if (col.DataType == System.Type.GetType("System.DateTime"))
{
app.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
else
{
app.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
range.HorizontalAlignment = XlVAlign.xlVAlignCenter;
range.RowHeight = 80;
}
}
int rowSum = rowIndex + 1;
//取得整个报表的标题
app.Cells[1, 1] = str;
sheet.get_Range(app.Cells[1, 1], app.Cells[1, dv.Table.Columns.Count]).Merge(0); //标题合并
sheet.get_Range(app.Cells[1, 1], app.Cells[1, dv.Table.Columns.Count]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//标题居中
app.Visible = true;
book.SaveCopyAs(Server.MapPath("a.xlsx"));
dv = null;
book.Close(false,null,null);
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(book );
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
book = null;
app = null;
sheet = null;
GC.Collect();
string path = Server.MapPath("a.xlsx");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataView dv = bl.GetView();
OutputExcel(dv,"软件安全性报表");
}
}
- 导出Excel (application方法)
- Excel.Application 导出Excel
- 关于Excel表格导出方法--application/vnd.ms-excel
- 导出excel的方法
- 导出excel的方法
- php导出Excel方法
- 导出excel的方法
- EXCEL导出方法
- 通用 Excel 导出方法
- Excel导出方法选择
- GridView导出Excel方法
- WPF 导出EXCEL 方法
- 导出excel的方法
- 导出Excel方法
- 导出EXCEL方法汇总
- 导出excel信息方法
- DataGrid导出EXCEL方法
- C# 导出Excel方法
- web.config中access数据库相对路径实现方式
- 晒一晒自己的“图片验证码”程序 asp.net
- oracle数据库函数
- 正则表达式(参考百度词条)
- JAVASCRIPT 获取 XML 文件内容
- 导出Excel (application方法)
- 多选复选框 传值到文本框
- __asm__ __volatile__内嵌汇编用法简述
- Tomcat 使用Log4j日志。
- 造成ORA-01843 无效的月份的一些原因
- OpenMax多媒体引擎
- 如何通过培养健康的习惯来实现目标
- CSS控制DIV永远固定在页面底部
- SVN分支与合并