asp.net导出带有筛选功能的excel的难点,有点难度 如题,网上搜了一下,全部是一般的导出.我的客户的要求是带有自动筛选功能的. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 导出带筛选的excel估计要调用com.......关注 给你一个思路.我以前也要实现类似的功能,你把你附上的那个excel文件除了标题留下之外,其它的内容全部删除到,然后将这个Excel文件做为一个模板,在要生成Excel文件的时候,复制一下这个模板,放到指定位置,然后再填充Excel,这样子就可以实现你要的功能了. 如果格式固定的话,Tom1984的方法不失为一个好的方法.昨天从老外的一个方法学习了一下,我的方法 public void CreateExcel(System.Data.DataTable dtData, string FileName, HttpResponse response) { GC.Collect(); Excel.Application excel;// = new Application(); int rowIndex = 1; int colIndex = 0; Excel._Workbook xBk; Excel._Worksheet xSt; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel._Worksheet)xBk.ActiveSheet; excel.Cells[1, 1] = dtData.Columns[0].ColumnName; excel.Cells[1, 2] = dtData.Columns[1].ColumnName; excel.Cells[1, 3] = dtData.Columns[2].ColumnName; excel.Cells[1, 4] = dtData.Columns[3].ColumnName; excel.Cells[1, 5] = dtData.Columns[4].ColumnName; foreach (DataRowView row in dtData.DefaultView) { rowIndex++; colIndex = 0; foreach (DataColumn col in dtData.Columns) { colIndex++; if (col.DataType == System.Type.GetType("System.DateTime")) { excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐 } else if (col.DataType == System.Type.GetType("System.String")) { excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString(); //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 } else { excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } } xSt.Cells.AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true); // xSt.get_Range("A1","H1").AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true); excel.Visible = true; xBk.SaveCopyAs(Server.MapPath("../") + this.xlfile.Text + ".xls"); xBk.Close(false, null, null); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null; excel = null; xSt = null; GC.Collect(); string path = Server.MapPath("../" + this.xlfile.Text + ".xls"); 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.Flush(); // 把文件流发送到客户端 Response.WriteFile(file.FullName); // 停止页面的执行 Response.End(); } asp.net上传头像在线裁减 List<T>的查找,修改,删除要如何实现? html里嵌入多个htm页面 嵌套表格实现整个页面的自动跳转 支持doughty 的勇敢者论坛源码,特散分200 web 打包程序中能将web service 和Windows service一起打包安装。 如何在javascript里面写系统退出和注销代码 VS.Net可以设置自己的关键字如自己的名字如JEO在代码中高亮显示吗? 有没有好的方法在.vb的文件中写代码,弹出警告一类的窗口? 哪位大虾帮忙看一下错在哪里呢? .net的一个小问题 JSP页面如何获取.NET传过来的参数?
关注
昨天从老外的一个方法学习了一下,我的方法
public void CreateExcel(System.Data.DataTable dtData, string FileName, HttpResponse response)
{
GC.Collect();
Excel.Application excel;// = new Application();
int rowIndex = 1;
int colIndex = 0; Excel._Workbook xBk;
Excel._Worksheet xSt; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel._Worksheet)xBk.ActiveSheet;
excel.Cells[1, 1] = dtData.Columns[0].ColumnName;
excel.Cells[1, 2] = dtData.Columns[1].ColumnName;
excel.Cells[1, 3] = dtData.Columns[2].ColumnName;
excel.Cells[1, 4] = dtData.Columns[3].ColumnName;
excel.Cells[1, 5] = dtData.Columns[4].ColumnName; foreach (DataRowView row in dtData.DefaultView)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dtData.Columns)
{
colIndex++;
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
//xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
//xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
xSt.Cells.AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
// xSt.get_Range("A1","H1").AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
excel.Visible = true;
xBk.SaveCopyAs(Server.MapPath("../") + this.xlfile.Text + ".xls"); xBk.Close(false, null, null); excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath("../" + this.xlfile.Text + ".xls"); 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.Flush(); // 把文件流发送到客户端
Response.WriteFile(file.FullName); // 停止页面的执行
Response.End();
}