Using Content control to print MSSQL Table

  • Thread starter Thread starter oderis
  • Start date Start date
O

oderis

Guest
I want to print a table from the database and print on some content control on an MS word document.

I am very new to using content control to print out tables from a Database, Can i use SdtElement?

My code looks like this (Although I never used the SdtElement) I just need to be clear on some few things

Code looks like this :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace TablePrintExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
PrintTablesFromDB();
}

public static void AddTable(string fileName, string[,] data)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{
var doc = document.MainDocumentPart.Document;
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
TableProperties props = new TableProperties(
new TableBorders(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 },
new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6 }
));
table.ClearAllAttributes();
table.AppendChild<TableProperties>(props);
for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
string[] datas = data[i, j].ToString().Split('\n');
for (int k = 0; k < datas.Length; k++)
{
tc.Append(new Paragraph(new Run(new Text(datas[k]))));
tc.Append(new TableCellProperties(new TableCellVerticalAlignment { Val = TableVerticalAlignmentValues.Center }));
}
//tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}

private void PrintTablesFromDB()
{
string constring = @"Data Source=DLX;Initial Catalog=K2;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string query = "select * from [EmployeeInfoDB].[dbo].[EmpDbTable]";
using (SqlDataAdapter da = new SqlDataAdapter(query, con))
{
try
{
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Employee Name"), new DataColumn("Desg") });
string col1 = dt.Rows[0]["EmployeeName"].ToString();
string col2 = dt.Rows[0]["Designation"].ToString();
string[,] Tablero = new string[1, 2] { { col1, col2 } };
AddTable(@"C:\Users\Administrator\Desktop\TableTest.docx", Tablero);
Process.Start(@"C:\Users\Administrator\Desktop\TableTest.docx");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
}



How can I do something like this??

Continue reading...
 
Back
Top