zoukankan      html  css  js  c++  java
  • 将DataSet数据到入到Excel

    protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (this.dalListContract.Items.Count > 0)
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("序号");
                dt.Columns.Add("名称");
                if (null != Cache["toExcel"])
                {
                    DataTable temp = (DataTable)Cache["toExcel"];
                    for (int i = 0; i < temp.Rows.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr[0] = i + 1;
                        dr[1] = temp.Rows[i]["code"];
                        dr[2] = temp.Rows[i]["classne"];
                        dt.Rows.Add(dr);
                    }
                    ds.Tables.Add(dt);
                    ExportExcel(ds, "test");
                }
            }
           

        }
        private void ExportExcel(DataSet ds, string filename)
        {
            Microsoft.Office.Interop.Excel.Application oExcel;
            oExcel = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                Microsoft.Office.Interop.Excel.Workbook oBook;
                object oMissing = System.Reflection.Missing.Value;
                oBook = oExcel.Workbooks.Add(oMissing);
                HttpResponse response = HttpContext.Current.Response;
                int lie = ds.Tables[0].Columns.Count;
                int hang = ds.Tables[0].Rows.Count;

                int i, j, t;
                string panduanstring = "";
                i = 1;
                for (j = 0; j < lie; j++)//标题
                {
                    oExcel.Cells[1, i++] = ds.Tables[0].Columns[j].ColumnName;
                }

                t = 1;
                for (i = 0; i < hang; i++)//内容
                {
                    for (j = 0; j < lie; j++)
                    {

                        panduanstring = ds.Tables[0].Rows[i][j].ToString();
                        if (panduanstring.GetType().ToString() == "System.String")
                        {
                            oExcel.Cells[i + 2, t++] = "'" + ds.Tables[0].Rows[i][j].ToString();
                        }
                        else
                            oExcel.Cells[i + 2, t++] = ds.Tables[0].Rows[i][j].ToString();

                    }
                    t = 1;
                }

                oExcel.Visible = true;
                oBook.Saved = true;
                oExcel.UserControl = false;

                string path = Server.MapPath("excel/");
                string mm = path + filename + ".xls";

                oExcel.ActiveWorkbook.SaveCopyAs(mm);
                oExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
                GC.Collect();
                response.Redirect("excel/" + filename + ".xls");

            }
            catch (Exception e)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
                GC.Collect();
            }
        }

  • 相关阅读:
    微信小程序真机预览跟本地不同的问题。原文地址:https://blog.csdn.net/qq_27187991/article/details/69664247/
    java 获取目标时间到当前时间中间的月份和每月最大时间
    关于事物与性能的测试
    两步搞定一台电脑同时开启多个tomcat
    一个java新手配置IIS服务器的血泪史
    16、指数加权平均算法介绍及偏差修正
    15、优化算法之Mini-batch 梯度下降法
    14、改善深层神经网络之梯度检验
    13、改善深层神经网路之权重初始化方法
    12、改善深层神经网络之梯度消失与梯度爆炸及其具体解决方案
  • 原文地址:https://www.cnblogs.com/ymyglhb/p/1441445.html
Copyright © 2011-2022 走看看