Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - Работа с данными

Страница: 1 |

 

  Вопрос: Экспорт данных из DataGridView в Word Добавлено: 10.04.09 20:17  

Автор вопроса:  Лёня
Подскажите пожалуйста!!!или укажите путь где уже рассматривался этот вопрос!!Очень нужно!!
на форме есть датагрид, необходимо всё его содержимое вывести в ворд со всеми записями.
Заранее благодарен Вам!!

Ответить

  Ответы Всего ответов: 2  

Номер ответа: 1
Автор ответа:
 Fatty



Вопросов: 0
Ответов: 55
 Профиль | | #1 Добавлено: 29.04.09 00:01
Как раз вчера парился с тем же самым вопросом
Посмотри может пригодится
У меня работает VS2005, MS Office 2003
  1.         /// <summary>
  2.         ///
  3.         /// </summary>
  4.         /// <param name="sender"></param>
  5.         /// <param name="e"></param>
  6.         private void btnExportWord_Click(object sender, EventArgs e)
  7.         {
  8.             try
  9.             {
  10.                 DataTable dt = table;// <==extrafluous, just for imaging
  11.                 wdApp = new Microsoft.Office.Interop.Word.Application();
  12.                 wdApp.Visible = true;
  13.  
  14.                 wdDoc = (Microsoft.Office.Interop.Word.Document)wdApp.Documents.Add(ref deflt, ref deflt, ref deflt, ref deflt);
  15.                 //wdDoc.Open();
  16.                 wdDoc.Activate();
  17.                 wdDoc = wdApp.ActiveDocument;
  18.                 object start = 0;
  19.                 Microsoft.Office.Interop.Word.Range rng = wdDoc.Range(ref start, ref start);
  20.  
  21.  
  22.                 rng.InsertBefore("Some Text Line Goes Here");
  23.  
  24.                 rng.Font.Name = "Arial";
  25.                 rng.Font.Size = 12;
  26.                 Microsoft.Office.Interop.Word.WdParagraphAlignment alg = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  27.                 wdDoc.Paragraphs[1].Alignment = alg;
  28.                 rng.InsertParagraphAfter();
  29.                 rng.InsertParagraphAfter();
  30.                 rng.SetRange(rng.End, rng.End);
  31.                 Word.Table tbl = wdDoc.Tables.Add(wdDoc.Paragraphs[2].Range, dt.Rows.Count + 1, dt.Columns.Count, ref deflt, ref deflt);
  32.                 ExpGridToWord(dt, tbl, "Table Title Goes Here");
  33.                 object filename = "C:\\MyTableDoc.doc";
  34.                 wdDoc.SaveAs(
  35.                     ref filename, ref deflt, ref deflt, ref deflt, ref deflt,
  36.                     ref deflt, ref deflt, ref deflt, ref deflt, ref deflt,
  37.                     ref deflt, ref deflt, ref deflt, ref deflt, ref deflt, ref deflt);
  38.                 wdApp.Quit(ref deflt, ref deflt, ref deflt);
  39.                 wdDoc = null;
  40.                 wdApp = null;
  41.                 GC.Collect();
  42.                 GC.WaitForPendingFinalizers();
  43.                 GC.Collect();
  44.                 GC.WaitForPendingFinalizers();
  45.             }
  46.             catch (Exception ex)
  47.             {
  48.                 MessageBox.Show(ex.Message +
  49.                     "\n_____________________________\n" +
  50.                     ex.StackTrace);
  51.             }
  52.         }
  53.  /// <summary>
  54.  ///
  55.  /// </summary>
  56.  /// <param name="dt"></param>
  57.  /// <param name="tbl"></param>
  58.  /// <param name="title"></param>
  59.         private void ExpGridToWord(DataTable dt, Word.Table tbl,string title)
  60.         {
  61.             int row = 1;
  62.             Microsoft.Office.Interop.Word.WdParagraphAlignment alg = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  63.             tbl.Cell(row, 1).Range.Text = title;
  64.             tbl.Rows[1].Cells.Merge();
  65.             tbl.Rows[1].Range.Paragraphs[1].Alignment = alg;          
  66.             row = 2;          
  67.             for (int i = 0; i < dt.Columns.Count; i++)
  68.             {
  69.                 tbl.Cell(row, i+1).Range.Text = dt.Columns.ColumnName;
  70.             }
  71.  
  72.             row = 2;
  73.             int col = 0;
  74.  
  75.             for (int i = 0; i < dt.Rows.Count; i++)
  76.             {
  77.                 col = 1;
  78.                 for (int j = 0; j < dt.Columns.Count; j++)
  79.                 {
  80.                     tbl.Cell(row,col).Range.Text = dt.Rows[j].ToString();//<=======
  81.                     col++;
  82.                 }
  83.                 
  84.                 row++;
  85.             }
  86.         }


~'J'~

Ответить

Номер ответа: 2
Автор ответа:
 Fatty



Вопросов: 0
Ответов: 55
 Профиль | | #2 Добавлено: 29.04.09 01:40
Нашел ошибку используй этот вариант
  1.         /// <summary>
  2.         ///
  3.         /// </summary>
  4.         /// <param name="sender"></param>
  5.         /// <param name="e"></param>
  6.         private void btnExportWord_Click(object sender, EventArgs e)
  7.         {
  8.             try
  9.             {
  10.                 DataTable dt = table;
  11.                 wdApp = new Microsoft.Office.Interop.Word.Application();
  12.                 wdApp.Visible = true;
  13.  
  14.                 wdDoc = (Microsoft.Office.Interop.Word.Document)wdApp.Documents.Add(ref deflt, ref deflt, ref deflt, ref deflt);
  15.                 wdDoc.Activate();
  16.                 wdDoc = wdApp.ActiveDocument;
  17.                 object start = 0;
  18.                 Microsoft.Office.Interop.Word.Range rng = wdDoc.Range(ref start, ref start);
  19.  
  20.  
  21.                 rng.InsertBefore("Some Text Line Goes Here");
  22.  
  23.                 rng.Font.Name = "Calibri";
  24.                 rng.Font.Size = 14;
  25.                 rng.Font.Bold = 1;
  26.                 rng.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
  27.                 Microsoft.Office.Interop.Word.WdParagraphAlignment alg = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  28.                 wdDoc.Paragraphs[1].Alignment = alg;
  29.                 rng.InsertParagraphAfter();
  30.                 rng.InsertParagraphAfter();
  31.                 rng.SetRange(rng.End, rng.End);
  32.                 //
  33.                 Word.Table tbl = wdDoc.Tables.Add(wdDoc.Paragraphs[2].Range, dt.Rows.Count + 2, dt.Columns.Count, ref deflt, ref deflt);
  34.                 for (int n = 2; n <= tbl.Rows.Count; n++)
  35.                 {
  36.                     if (n % 2 == 0)
  37.                     {
  38.                         tbl.Rows[n].Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorGray10;
  39.                     }
  40.                     else
  41.                     {
  42.                         tbl.Rows[n].Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorGray20;
  43.                     }
  44.                 }
  45.                 tbl.Range.Font.Name = "Tahoma";
  46.                 tbl.Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
  47.                 tbl.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
  48.                 tbl.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
  49.                 tbl.Borders.InsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
  50.                 tbl.Borders.OutsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
  51.                 tbl.Rows[1].Range.Cells[1].Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrown;
  52.                 tbl.Rows[1].Range.Cells[1].Range.Bold = 1;
  53.                 tbl.Rows[1].Range.Cells[1].Range.Borders.Shadow = false;
  54.                 //
  55.                 ExpGridToWord(dt, tbl, "Table Title Goes Here");
  56.                 object filename = "C:\\MyTableDoc.doc";
  57.                 wdDoc.SaveAs(
  58.                     ref filename, ref deflt, ref deflt, ref deflt, ref deflt,
  59.                     ref deflt, ref deflt, ref deflt, ref deflt, ref deflt,
  60.                     ref deflt, ref deflt, ref deflt, ref deflt, ref deflt, ref deflt);
  61.                 wdApp.Quit(ref deflt, ref deflt, ref deflt);
  62.                 wdDoc = null;
  63.                 wdApp = null;
  64.                 GC.Collect();
  65.                 GC.WaitForPendingFinalizers();
  66.                 GC.Collect();
  67.                 GC.WaitForPendingFinalizers();
  68.             }
  69.             catch (Exception ex)
  70.             {
  71.                 MessageBox.Show(ex.Message +
  72.                     "\n_____________________________\n" +
  73.                     ex.StackTrace);
  74.             }
  75.         }
  76.  /// <summary>
  77.  ///
  78.  /// </summary>
  79.  /// <param name="dt"></param>
  80.  /// <param name="tbl"></param>
  81.  /// <param name="title"></param>
  82.         private void ExpGridToWord(DataTable dt, Word.Table tbl,string title)
  83.         {
  84.             int row = 1;
  85.             Microsoft.Office.Interop.Word.WdParagraphAlignment alg = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  86.             tbl.Cell(row, 1).Range.Text = title;
  87.             tbl.Rows[1].Cells.Merge();
  88.             tbl.Rows[1].Range.Paragraphs[1].Alignment = alg;          
  89.             row = 2;          
  90.             for (int i = 0; i < dt.Columns.Count; i++)
  91.             {
  92.                 tbl.Cell(row, i+1).Range.Text = dt.Columns.ColumnName;
  93.             }
  94.  
  95.             row = 3;
  96.             int col = 0;
  97.  
  98.             for (int i = 0; i < dt.Rows.Count; i++)
  99.             {
  100.                 col = 1;
  101.                 for (int j = 0; j < dt.Columns.Count; j++)
  102.                 {
  103.                     tbl.Cell(row,col).Range.Text = dt.Rows[j].ToString();
  104.                     col++;
  105.                 }
  106.                 
  107.                 row++;
  108.             }
  109.         }


~'J'~

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам