Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1.         private void uploadWord_Click(object sender, EventArgs e)
  2.         {
  3.             DataTable dt = new DataTable();
  4.             dt.Columns.AddRange(new DataColumn[4] { new DataColumn("ID", typeof(string)), new DataColumn("ФИО пациента", typeof(string)), new DataColumn("Лечаший врач", typeof(string)), new DataColumn("Диагноз", typeof(string)) });
  5.             dt.Rows.Add("ID", "ФИО пациента", "Лечащий врач", "Диагноз");
  6.             for (int i = 0; i < entryGrid.RowCount; i++)
  7.             {
  8.                 dt.Rows.Add(entryGrid.Rows[i].Cells[0].Value.ToString(), entryGrid.Rows[i].Cells[1].Value.ToString(), entryGrid.Rows[i].Cells[2].Value.ToString(), entryGrid.Rows[i].Cells[0].Value.ToString());
  9.             }
  10.             dt.Rows.Add("", "", "", "");
  11.             dt.Rows.Add("ID", "ФИО врача", "E-mail", "Специальность");
  12.             for (int i = 0; i < entryGrid.RowCount; i++)
  13.             {
  14.                 dt.Rows.Add(doctorsGrid.Rows[i].Cells[0].Value.ToString(), doctorsGrid.Rows[i].Cells[1].Value.ToString(), doctorsGrid.Rows[i].Cells[2].Value.ToString(), doctorsGrid.Rows[i].Cells[0].Value.ToString());
  15.             }
  16.             string[] splitString = { "bin" };
  17.             // Path of blank document.
  18.             CreateWordDocument("output.docx", dt);
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.             //Открываем полученный документ
  26.             MessageBox.Show("Успешно экспортировано! Открываем документ...");
  27.             Process.Start("output.docx");
  28.         }
  29.         public void CreateWordDocument(string filePath, DataTable data)
  30.         {
  31.             WordprocessingDocument doc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document);
  32.             MainDocumentPart mainDocPart = doc.AddMainDocumentPart();
  33.             mainDocPart.Document = new Document();
  34.             Body body = new Body();
  35.             mainDocPart.Document.Append(body);
  36.  
  37.             DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
  38.             for (int i = 0; i < data.Rows.Count; ++i)
  39.             {
  40.                 TableRow row = new TableRow();
  41.                 for (int j = 0; j < data.Columns.Count; j++)
  42.                 {
  43.                     TableCell cell = new TableCell();
  44.                     cell.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(data.Rows[i][j].ToString()))));
  45.                     cell.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "3000" }, new TableBorders(new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
  46.                         new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 12 },
  47.                         new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 12 },
  48.                         new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 12 },
  49.                         new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 12 },
  50.                         new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 12 })));
  51.  
  52.                     row.Append(cell);
  53.                 }
  54.                 table.Append(row);
  55.             }
  56.             body.Append(table);
  57.             doc.MainDocumentPart.Document.Save();
  58.             doc.Dispose();
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement