同上

解决方案 »

  1.   

    http://topic.csdn.net/u/20091127/13/4c7091b8-31d4-44bf-b21c-476cffde7873.html
      

  2.   

    http://www.cnblogs.com/ericfine/archive/2008/10/24/1318923.html
      

  3.   

    你问了也不结贴,谁还愿意回答你啊http://topic.csdn.net/u/20100925/14/d04a798e-edbb-493d-9ded-ead30e3473bb.html
      

  4.   

    本帖最后由 net_lover 于 2010-09-26 15:56:19 编辑
      

  5.   


        private void Export()
        {
            string strTempFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";
            string strModName = Server.MapPath(".") + @"\Template.xls";
            string strDownFileName = @"./" + strTempFileName;
            string strFileName = Server.MapPath(".") + @"\" + strTempFileName;        Excel.Application m_objExcel  = new Excel.Application();
            m_objExcel.Workbooks.Open(strModName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);        Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
            Excel.Workbook m_objBook = m_objExcel.ActiveWorkbook;
            Excel.Worksheet sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
            //for (int i = 0; i < total.Rows.Count; i++)
            //{
            //    for (int j = 0; j < total.Columns.Count; j++)
            //    {
            //        Excel.Range range = sheet.get_Range(GetAix(j + 1, i + 1), miss);
            //        range.set_Value(miss, total.Rows[i][j].ToString());        //        //string aix = y + x.ToString();
            //        //Excel.Range range = sheet.get_Range(aix, miss);
            //        //range.set_Value(miss, text);
            //    }
            //}        //Excel.ApplicationClass excel;
            //Excel._Workbook xBk;
            //Excel._Worksheet xSt;        //Excel.Range r = xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]);
          
            for (int i = 0; i < total.Rows.Count; i++)
            {
                for (int j = 0; j < total.Columns.Count; j++)
                {
                    Excel.Range range = sheet.get_Range(m_objExcel.Cells[i + 1, j + 1], m_objExcel.Cells[i + 1, j + 1]);
                    if (total.Columns[j].ColumnName == "成绩")
                    {
                        range.set_Value(miss, total.Rows[i][j].ToString());
                        
                    }
                    else
                    {
                        range.set_Value(miss, total.Rows[i][j].ToString());
                    }
                }
            }
                m_objBook.SaveAs(@strFileName, miss, miss, miss, miss,
               miss, Excel.XlSaveAsAccessMode.xlNoChange,
               Excel.XlSaveConflictResolution.xlLocalSessionChanges, miss, miss, miss, miss);
                m_objExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel);
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks);
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);
                m_objExcel = null;
                m_objBooks = null;
                m_objBook = null;
                sheet = null;
                GC.Collect();            Response.Redirect(strDownFileName, false);
            
        }
        protected static System.Data.DataTable total;    private object miss = Missing.Value;
        private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 初始化绑定
            if (!IsPostBack)
            {
                //创建和声明DataTable对象
                total = new DataTable();
                //给DataTable对象添加DataColumn对象
                total.Columns.Add(new DataColumn("班组", typeof(string)));
                total.Columns.Add(new DataColumn("姓名", typeof(string)));
                total.Columns.Add(new DataColumn("学科", typeof(string)));
                total.Columns.Add(new DataColumn("成绩", typeof(string)));
                for (int i = 0; i < 16; i++)
                {
                    //声明DataRow对象
                    DataRow dr = total.NewRow();
                    //给DataRow对象中的各列赋值
                    dr["班组"] = "一班" + i + ":" + 1;
                    dr["姓名"] = " 李宏" + i + ":" + 1;
                    dr["学科"] = "语文 " + i + ":" + 1;
                    dr["成绩"] = "98000001222222222222222222";
                    //将DataRow对象添加到DataTable对象中
                    total.Rows.Add(dr);
                    dr = total.NewRow();
                    dr["班组"] = "一班" + i + ":" + 2;
                    dr["姓名"] = " 刘京生" + i + ":" + 2;
                    dr["学科"] = "语文 " + i + ":" + 2;
                    dr["成绩"] = "00000000000000000000000096";
                    total.Rows.Add(dr);
                }        }
            #endregion
        }using System.Reflection;
      

  6.   

    再次给你说明一下,
    Excel.Application
    这种方法不适合在虚拟主机上使用,如果没有自己的服务器,你直接生成html的兼容格式或者xml格式
    答案已经在上一个帖子了
      

  7.   

            public static void TableToExcel(System.Data.DataTable tb)
            {
                string Filename = "CardExcel";
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                if ((tb != null))
                {
                    context.Response.Clear();
                    context.Response.Charset = "GB2312";
                    context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    context.Response.ContentType = "application/ms-excel";
                    context.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename, System.Text.Encoding.GetEncoding("GB2312")) + ".xls\"");                CultureInfo cult = new CultureInfo("zh-CN", true);
                    StringWriter sw = new StringWriter(cult);
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    DataGrid dgrid = new DataGrid();
                    dgrid.DataSource = tb.DefaultView;
                    dgrid.AllowPaging = false;
                    dgrid.DataBind();
                    htw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
                    dgrid.RenderControl(htw);
                    context.Response.Write(sw.ToString());
                    context.Response.End();
                }
            }