用户建立一个excel   (excel有它的样式)
然后我们按照这个excel模板导出数据
有人做过这个样的东西吗   很难啊

解决方案 »

  1.   

    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw);   GridView1.AllowPaging = false; GridView1.DataBind();   //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");   //Apply style to Individual Cells GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");     for (int i = 0; i < GridView1.Rows.Count;i++ ) {     GridViewRow row = GridView1.Rows[i];       
      

  2.   

    不是这样的   你这个是自己设置格式   我需要的是用户自己设置样式  然后按照用户设置的样式导出excel
      

  3.   

    那你得先弄懂导出Excel原理,然后才能做"按照用户设置的样式导出"
    楼主还是先弄懂一个简单的导出例子吧.
    这个问题,别人没办法给你代码,只能给例子
      

  4.   

    简单导出我已经弄好了  现在需要的是用户自定义导出   就是用户给模板  
    我找到思路了 
    /// <summary>
        /// 把table中的数据导出到excel中去
        /// </summary>
        /// <param name="dt">要导入的table</param>
        /// <param name="maxcount">模板excel中,一个sheet要显示的行数,如果数据多一个sheet中的最大值,会自动生成新的sheet</param>
        private void ExportExcel(System .Data .DataTable dt,int maxcount)
        {
            Random ran = new Random();
            string fileautoname = Server.MapPath("~/") + "Files\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 999) + ".xls";//给新文件命名
            string filepath = Server.MapPath("~/") + "EXCEL_Template\\Excel\\模板.xls";//模板文件路径
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.Application application = new Application();
            Workbook workbook = application.Workbooks.Open(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            Worksheet worksheet;
            worksheet = (Worksheet)workbook.Sheets.get_Item(1);        
            DataView dv = dt.DefaultView;
            dv.Sort = "id asc"; //table 中的数据按id升序排列
            System.Data.DataTable table = dv.ToTable();
            
            int sheetcount = GetSheetCount(table.Rows.Count, maxcount);
            for (int count = 1; count < sheetcount; count++)
            {
                ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(count)).Copy(missing, workbook.Worksheets[count]);
            }
            List<object[,]> list = new List<object[,]>();
            object[,] ret;
            for (int count = 0; count < sheetcount; count++)
            {
                if (count == sheetcount - 1)
                {
                    ret = new object[table.Rows.Count - count * maxcount, table.Columns.Count - 2];
                    for (int i = 0; i < table.Rows.Count - count * maxcount; i++)
                    {
                        for (int j = 0; j < table.Columns.Count - 2; j++)
                        {
                            ret[i, j] = table.Rows[count * maxcount + 1][j];
                        }
                    }
                    list.Add(ret);
                }
                else
                {
                    ret = new object[maxcount, table.Columns.Count - 2];
                    for (int i = 0; i < maxcount; i++)
                    {
                        for (int j = 0; j < table.Columns.Count - 2; j++)
                        {
                            ret[i, j] = table.Rows[i + count * maxcount][j];
                        }
                    }
                    list.Add(ret);
                }
            }        object[,] obj;
            for (int p = 0; p < list.Count; p++)
            {
                worksheet = (Worksheet)workbook.Sheets.get_Item(p + 1);
                obj = list[p];
                string cn = "L" + (obj.GetLength(0) + 2).ToString(); //设置填充区域
                worksheet.get_Range("A3", cn).FormulaR1C1 = obj;
            }
            workbook.SaveAs(fileautoname, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
            workbook.Close(missing, missing, missing);
            application.Quit();
            workbook = null;
        }
        /// <summary>
        ///  获取WorkSheet数量
        /// </summary>
        /// <param name="rowCount">记录总行数</param>
        /// <param name="rows">每WorkSheet行数</param>
        /// <returns></returns>
        private int GetSheetCount(int rowCount,int rows)
        {
            int n = rowCount % rows;        //余数        if(n == 0)
                return rowCount / rows;
            else
                return Convert.ToInt32(rowCount / rows) + 1;
        }
      

  5.   

    打开excel模板,通过二维数组赋值给单元格
      

  6.   

    这种性能不是很好,记得CodeProject里有一个C#实现读写Excel二进制(BIFF8)格式的例子,非常好。可以去搜索一下。