封装类 导出gridview为word

来源:互联网 发布:电视软件应用市场 编辑:程序博客网 时间:2024/05/29 21:34

 public static void Export_Word(Page page, GridView gv, string Title, int ColumnCount)
    {
        int RowCount = gv.Rows.Count + 2;
        object missing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application myword_app = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document mydoc = new Microsoft.Office.Interop.Word.Document();
        myword_app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        mydoc = myword_app.ActiveDocument;

        Microsoft.Office.Interop.Word.Table table;

        table = mydoc.Tables.Add(myword_app.Selection.Range, RowCount, ColumnCount, ref missing, ref missing);
        #region 标题
        table.Cell(1, 1).Range.Text = Title;
        table.Cell(1, 1).Row.Height = 30;
        table.Cell(1, 1).Range.Font.Size = 16;
        table.Cell(1, 1).Range.Font.Name = "华文中宋";
        //Microsoft.Office.Interop.Word.Range MergerRange = new Microsoft.Office.Interop.Word.Range(table.Cell[1, 1],table(1,ColumnCount));
        if (table.Rows[1].Cells.Count > 1)
            //table.Rows[1].Range.Move((Object)table.Cell(1, 2), (Object)(ColumnCount - 1));
            table.Rows[1].Cells.Merge();
        #endregion

        #region 表列名
        for (int i = 1; i <= ColumnCount; i++)
        {
            table.Cell(2, i).Range.Text = gv.HeaderRow.Cells[i - 1].Text;
        }       
        #endregion

        #region 填充表内容
        for (int i = 0; i < gv.Rows.Count; i++)
            for (int j = 0; j < ColumnCount; j++)
            {
                try
                {
                    table.Cell((i + 3), (j + 1)).Range.Text = ((TextBox)(gv.Rows[i].Cells[j].Controls[1])).Text;
                }
                catch
                {
                    table.Cell((i + 3), (j + 1)).Range.Text = gv.Rows[i].Cells[j].Text;
                }
            }
        #endregion

        myword_app.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
        myword_app.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

        #region 保存
        string path = page.Server.MapPath(".");
        string filename = Title + ".doc";
        object filepath = path + "//" + filename;
        mydoc.SaveAs(ref filepath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        mydoc.Close(ref missing, ref missing, ref missing);
        myword_app.Quit(ref missing, ref missing, ref missing);
        #endregion

        #region 下载

        FileInfo fileInfo = new FileInfo((string)filepath);
        HttpResponse Response = page.Response;
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8).ToString());
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        System.IO.FileInfo file = new System.IO.FileInfo((string)filepath);
        if (file.Exists)
        {
            file.Delete();
        }
        Response.End();
        #endregion
    }

原创粉丝点击