日期:2014-05-18  浏览次数:21156 次

c#WinForm程序将DataGridView数据写入WORD文档问题!
我想把DataGridView中的数据导出到一个WORD文档 在文档中创立一个Table 然后为table的每个单元格写值 行是行 但是速度实在是太慢了 100多行的数据 跑的CPU都到100%了 希望高手帮忙搞定下!
C# code

        /// <summary>
        /// 导出Word
        /// </summary>
        /// <param name="dt">导出的数据DataTable</param>
        /// <param name="isColname">是否显示列名</param>
        public static void OutPutWordDT(DataTable dt, bool isColname)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Word.Application oword = new Word.Application();//word Application
            Word.Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//文档
            odoc.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
           
            try
            {
                //在word以表格形式存储数据
                Word.Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing);
                otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式
                otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框样式
               
                if (isColname)//列名称
                {
                    int intcol = 0;
                    for (int ii = 0; ii < dt.Columns.Count; ii++)
                    {
                        intcol += 1;
                        otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName;
                        otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }

                //写表格内容
                int intRow = 1;
                for (int ii = 0; ii < dt.Rows.Count; ii++)
                {
                    intRow += 1;
                    int intCol = 0;
                    for (int jj = 0; jj < dt.Columns.Count; jj++)
                    {
                        intCol += 1;
                        otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString();
                        otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
                    }
                }
                oword.Visible = true;
            }
            catch (Exception) { }
            finally
            {
                System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
                for (int i = 0; i < CurrentProcess.Length; i++)
                {
                    if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
                    {
                        try
                        {
                            CurrentProcess[i].Kill();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }


------解决方案--------------------
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataGrid("application/ms-excel", "城市品牌报表.xls"); 
}

private void ExportDataGrid(string FileType, string FileName) //从DataGrid导出
{
Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();