如题,网上搜了一下,全部是一般的导出.我的客户的要求是带有自动筛选功能的.

解决方案 »

  1.   

    导出带筛选的excel估计要调用com.......
    关注
      

  2.   

       给你一个思路.我以前也要实现类似的功能,你把你附上的那个excel文件除了标题留下之外,其它的内容全部删除到,然后将这个Excel文件做为一个模板,在要生成Excel文件的时候,复制一下这个模板,放到指定位置,然后再填充Excel,这样子就可以实现你要的功能了.
      

  3.   

    如果格式固定的话,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();
        }