zoukankan      html  css  js  c++  java
  • ASP.NET(C#)将数据导出到Word或Excel

    1) ASP.NET(C#)将数据导出到Word或Excel
    命名空间:
    using System.IO;
    using System.Text;
    将DataGrid的数据导出到Excel
    string excelname="excel文件名";
    HttpContext.Current.Response.Charset = "GB2312";
    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".xls");
    dr1.Page.EnableViewState = false;
    StringWriter sw = new StringWriter();
    HtmlTextWriter tw = new HtmlTextWriter(sw);
    dr1.RenderControl(tw);
    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.End();
    将DataGrid的数据导出到Word
    string excelname="word文件名";
    HttpContext.Current.Response.Charset = "GB2312";
    HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
    HttpContext.Current.Response.ContentType = "application/ms-winword";
    HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".doc");
    dr1.Page.EnableViewState = false;
    StringWriter sw = new StringWriter();
    HtmlTextWriter tw = new HtmlTextWriter(sw);
    dr1.RenderControl(tw);
    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.End();
    ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件)
    // 注意,在Visual Studio2005平台下,如果使用GridView导出文件,
    //就必须重载VerifyRenderingInServerForm方法
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    /// <summary>
    /// 导出到文件的方法,
    /// </summary>
    /// <param name="Model">Model=1:导出为Execl,Model=2:导出为Word </param>
    private void toFiles(int Model)
    {
    string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss");
    System.Web.HttpContext HC = System.Web.HttpContext.Current;
    HC.Response.Clear();
    HC.Response.Buffer = true;
    HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文
    if (Model == 1)
    {
    //---导出为Excel文件
    HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");
    HC.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
    }
    else
    {
    //--- 导出为Word文件
    HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".doc");
    HC.Response.ContentType = "application/ms-word";//设置输出文件类型为Word文件。
    }
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
    this.GridView1.RenderControl(htw);
    HC.Response.Write(sw.ToString());
    HC.Response.End();
    }
    //-导出为Excel文件
    protected void ToExecl_Click(object sender, EventArgs e)
    {
    toFiles(1);
    }
    //-导出为Word文件
    protected void Button1_Click(object sender, EventArgs e)
    {
    toFiles(2);
    }
    (2)
    private byte [] CreateDOC ( Dictionary<string, string> dicHeader, Dictionary<int, Dictionary<string, string>> dicProductCollection ) {
    ........
    ........
    ........
    string fileName = path;
    if ( File.Exists ( fileName ) ) {     //图片存在就加进去
    object linkToFile = false;
    object saveWithDocument = true;
    b = wordApp.Selection.Find.Execute (
    ref from, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing );
    wordApp.Selection.Text = "";
    object anchor = wordDoc.Application.Selection.Range;
    wordDoc.Application.ActiveDocument.InlineShapes.AddPicture ( fileName, ref linkToFile, ref saveWithDocument, ref anchor ); // 添加图片
    float [] arrFix = GetFixImage ( fileName, 180 );
    if ( arrFix [0] != 0 && arrFix [1] != 0 ) { //设置宽度
    wordDoc.Application.ActiveDocument.InlineShapes [pictureCount].Width = arrFix [0];//图片宽度
    wordDoc.Application.ActiveDocument.InlineShapes [pictureCount].Height = arrFix [1];//图片高度
    }
    pictureCount++;
    } else {
    to = "";
    b = wordApp.Selection.Find.Execute (
    ref from, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref to,
    ref replaceOne, ref Nothing, ref Nothing, ref Nothing, ref Nothing ); //替换掉#image
    }
    }
    }
    }
    #endregion
    string folder = ConfigurationManager.AppSettings ["Folder"];
    string outPath = Server.MapPath ( folder );
    object name = DateTime.Now.Year.ToString () + "_" + DateTime.Now.Month.ToString () + "_" + DateTime.Now.Day.ToString () +
    "_" + DateTime.Now.Hour.ToString () + "_" + DateTime.Now.Minute.ToString () + "_" + DateTime.Now.Second.ToString () + ".doc";
    object filename = outPath + "\\" + name;
    if ( !Directory.Exists ( outPath ) ) {
    Directory.CreateDirectory ( outPath );
    }
    wordDoc.SaveAs ( ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing );
    wordDoc.Close ( ref Nothing, ref Nothing, ref Nothing );
    wordApp.Quit ( ref Nothing, ref Nothing, ref Nothing );
    GC.Collect ();
    byte [] bytes = File.ReadAllBytes ( filename.ToString () );
    return bytes;
    } catch ( SystemException ee ) {
    log.Error ( "OfferSheet Word Error:" + ee.ToString () );
    GC.Collect ();
    return null;
    }
    }
    private float [] GetFixImage ( string fileName ,float fixNumber) {
    float [] arrFix = new float [2];
    System.Drawing.Image imageCheck = System.Drawing.Image.FromFile ( fileName );
    if ( Convert.ToInt32 ( imageCheck.Width ) > 400 || Convert.ToInt32 ( imageCheck.Height ) > 400 ) {
    if ( imageCheck.Width > imageCheck.Height ) {         // 宽》高
    arrFix [0] = fixNumber;
    arrFix [1] = fixNumber * Convert.ToInt32 ( imageCheck.Height ) / Convert.ToInt32 ( imageCheck.Width );
    } else {
    arrFix [0] = fixNumber * Convert.ToInt32 ( imageCheck.Width ) / Convert.ToInt32 ( imageCheck.Height );
    arrFix [1] = fixNumber;
    }
    } else {
    arrFix [0] = 0;
    arrFix [1] = 0;
    }
    return arrFix;
    }
    }

    联盟快卖 商人,生意人,待创业人士在此可以共赢互利 期待你的加入 群号:140809277
  • 相关阅读:
    关于软件生命周期以及软件过程
    尴尬!Jsp内置对象
    软件工程导论的课后作业
    Bing Maps进阶系列五:通过DeepEarth的MiniMap控件为Bing Maps扩展迷你小地图
    Bing Maps进阶系列四:路由功能服务(RouteService)
    Bing Maps进阶系列三:使用地图图像服务(ImageryService)
    Bing Maps进阶系列二:使用GeocodeService进行地理位置检索
    Bing Maps进阶系列一:初识Bing Maps地图服务
    在C#中实现listbox的项上下移动(winform) 标准
    C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题
  • 原文地址:https://www.cnblogs.com/yexinw/p/2167084.html
Copyright © 2011-2022 走看看