• 导出到Excel


        
            ///// <summary>
            ///// 导出到Excel lichenghu
            ///// </summary>
            ///// <param name="dt"></param>
            //public static void ToExcel(DataTable dt)
            //{
            //    #region MyRegion

            //    List<string> columnlist = new List<string>();
            //    columnlist.Add("国家");
            //    columnlist.Add("省份");
            //    columnlist.Add("城市");
            //    columnlist.Add("学校名称");
            //    columnlist.Add("学校排名");
            //    columnlist.Add("专业名称");
            //    columnlist.Add("专业排名");
            //    columnlist.Add("学制");
            //    columnlist.Add("学位");
            //    columnlist.Add("学科");
            //    columnlist.Add("学位详细信息");
            //    columnlist.Add("GPA成绩");
            //    columnlist.Add("托福成绩");
            //    columnlist.Add("是否雅思");
            //    columnlist.Add("雅思成绩");
            //    columnlist.Add("是否Gre");
            //    columnlist.Add("Gre推荐分数");
            //    columnlist.Add("Gre说明");
            //    columnlist.Add("是否GRE单项成绩");
            //    columnlist.Add("Gre单项分数");
            //    columnlist.Add("是否GreSub");
            //    columnlist.Add("GreSub分数");
            //    columnlist.Add("是否Gmat");
            //    columnlist.Add("GMAT推荐分数");
            //    columnlist.Add("GMAT描述");
            //    columnlist.Add("是否需要分数认证");
            //    columnlist.Add("是否接受双录取");
            //    columnlist.Add("提前结束课程");
            //    columnlist.Add("信贷学费");
            //    columnlist.Add("总学费");
            //    columnlist.Add("是否有奖学金");
            //    columnlist.Add("注意事项");
            //    columnlist.Add("国际生比例");
            //    columnlist.Add("重要课程");
            //    columnlist.Add("学校偏好");
            //    columnlist.Add("学校特色");
            //    #endregion

            //    int num = 0;

            //    string sb = "";


            //    for (int i = 0; i < columnlist.Count; i++)
            //    {
            //        //sb = columnlist[i];
            //        sb = columnlist[i] + "\n";
            //    }


            //    foreach (DataRow dr in dt.Rows)
            //    {
            //        for (int i = 0; i < dt.Columns.Count; i++)
            //        {
            //            sb = sb + dr[i].ToString() + "\t";
            //        }
            //        sb = sb + "\n";
            //    }
            //    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=MyExcel.xls");
            //    HttpContext.Current.Response.Charset = "UTF-8";
            //    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            //    HttpContext.Current.Response.ContentType = "application/ms-excel";
            //    System.IO.StringWriter tw = new System.IO.StringWriter();
            //    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            //    hw.WriteLine(sb.ToString());
            //    HttpContext.Current.Response.Write(tw.ToString());
            //    HttpContext.Current.Response.End();
            //    hw.Flush();
            //    hw.Close();
            //    tw.Flush();
            //    tw.Close();
            //}


            ///// <summary>
            ///// 导出Excel
            ///// </summary>
            ///// <param name="dt">要导出的DataTable</param>
            //public void ExportToExcel(System.Data.DataTable dt)
            //{
            //    if (dt == null) return;
            //    Excel.Application xlApp = new Excel.Application();
            //    if (xlApp == null)
            //    {
            //        Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('无法创建Excel对象,可能您的机子未安装Excel!')</script>");

            //        return;
            //    }
            //    Excel.Workbooks workbooks = xlApp.Workbooks;
            //    Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            //    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
            //    worksheet.Name = "详细信息";
            //    Excel.Range range = null;
            //    long totalCount = dt.Rows.Count;
            //    long rowRead = 0;
            //    float percent = 0;
            //    //写入标题
            //    for (int i = 0; i < dt.Columns.Count; i++)
            //    {
            //        worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
            //        range = (Excel.Range)worksheet.Cells[1, i + 1];
            //        //range.Interior.ColorIndex = 15;//背景颜色
            //        range.Font.Bold = true;//粗体
            //        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//居中
            //        //加边框
            //        range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
            //        //range.ColumnWidth = 4.63;//设置列宽
            //        range.EntireColumn.AutoFit();//自动调整列宽
            //        //r1.EntireRow.AutoFit();//自动调整行高
            //    }
            //    //写入内容
            //    for (int r = 0; r < dt.Rows.Count; r++)
            //    {
            //        for (int i = 0; i < dt.Columns.Count; i++)
            //        {
            //            worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
            //            range = (Excel.Range)worksheet.Cells[r + 2, i + 1];
            //            range.Font.Size = 9;//字体大小
            //            //加边框
            //            range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
            //            range.EntireColumn.AutoFit();//自动调整列宽
            //        }
            //        rowRead++;
            //        percent = ((float)(100 * rowRead)) / totalCount;
            //    }
            //    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
            //    if (dt.Columns.Count > 1)
            //    {
            //        range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
            //    }
            //    try
            //    {
            //        workbook.Saved = false;
            //        //workbook.SaveAs(Server.MapPath("Excels/1.xls"), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //        //xlApp.Save(Server.MapPath("~/Manager/Excels/") + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            //        workbook.SaveCopyAs(System.Web.HttpRuntime.AppDomainAppPath + "Manager\\Excels\\" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            //    }
            //    catch (Exception ex)
            //    {
            //        Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('导出文件时出错,文件可能正被打开" + ex.Message + "!')</script>");
            //    }
            //    xlApp.Quit();
            //    GC.Collect();//强行销毁
            //    //      this.txt_hidden.Value = "";
            //    //Page.ClientScript.RegisterStartupScript(typeof(Page), "", "<script>alert('Excel!导出成功')</script>");

            //}

  • 相关阅读:
    动态添加placeholder
    texworks编码问题
    c++ 小知识(不定期更新)
    写一个简单的 Makefile
    ubuntu 忘了密码
    遍历各种组合
    分形
    C++ 尾递归优化
    基于 QQBot 实现简易 QQ 机器人
    遇到问题:在函数中开辟动态内存(已解决)
  • 原文地址:https://www.cnblogs.com/chenghu/p/2728274.html
Copyright © 2020-2023  润新知