DbToDocx Windows Workflow Activity

Windows Workflow Activity DbToDocx. Activity C#. Activity , DataSet, DataSet Word docx , Docx, Web Service WCF.
MS Visual Studio 2010 WCF + WorkFlow, Activity. WorkFlow, , Windows Form , DataGridView Microsofr Word.
1. Activity DbToDocx.
Activity InputArguments :
code
public class InputArguments
{
public string ConnectStrig{ get; set; }
public string SqlCommand{ get; set; }
public string nameFile{ get; set; }
}
2. Activity DbToDocx.
Activity OutputResult : byteDocx - Docx, dataSet Error - . . :dataSet Error, byteDocx - .
code
public class OutputResult
{
public string Error{ get; set; }
public DataSet dataSet{ get; set; }
public byte[] byteDocx{ get; set; }
}
3. Activity DbToDocx.
Activity DbToDocx , :
  • BeginExecute;
  • EndExecute.
, SQL BeginExecute, Activity EndExecute.
code
/*27.09.2012. Activity DbToDocx. 蠠
1. Activity Northwind DataSet,
, Word *.docx, DataSet Docx,
Word Docx.

2. InputArguments :
public string ConnectStrig{ get; set; }
public string SqlCommand{ get; set; }
public string nameFile{ get; set; }

3. OutputResult :
public string Error{ get; set; }
public DataSet dataSet{ get; set; }
public byte[] byteDocx{ get; set; }

4. InputArguments, OutputResult. DataSet
DataGridView, byte[] byteDocx Word ,
.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Activities;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Markup;
using System.ComponentModel;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ClassLibrary;

namespace ActivityLibrary
{
public sealed class DbToDocx : AsyncCodeActivity<OutputResult>
{
// Input Object
[RequiredArgument]
[DefaultValue(null)]
public InArgument<InputArguments> inputArguments { get; set; }

// Temporary variable for Input Arguments
private InputArguments inputObject= null;
private string connectStr= null;
private string cmdSQL= null;
private string nameFile= null;
private string activityDirectory= null;

//Output Object
private OutputResult OutRes = null;

// Temporary use variable
private DataSet dataSet= null;
private SqlConnection connect= null;
private SqlCommand command= null;
private SqlDataAdapter adapter= null;
private SqlDataReader reader= null;

private Word._Application word= null;
private Word._Document document= null;
Object missing = (Object)Type.Missing;

protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state)
{
try
{
// Input Object
inputObject = inputArguments.Get(context);
// Connection string
connectStr= inputObject.ConnectStrig;
// Command string
cmdSQL= inputObject.SqlCommand;
// Name File for write docx
nameFile= inputObject.nameFile;

// If connect string is empty
if (String.IsNullOrEmpty(connectStr)) throw new ArgumentNullException("Value", "Connection String is Empty");

// If command string is empty
if (String.IsNullOrEmpty(cmdSQL)) throw new ArgumentNullException("Value", "Command String is Empty");

// If nameFile string is empty
if (String.IsNullOrEmpty(nameFile)) throw new ArgumentNullException("Value", "File Name String is Empty");

connect = new SqlConnection(connectStr);
connect.Open();
command = new SqlCommand(cmdSQL, connect);
adapter = new SqlDataAdapter(command);
context.UserState = adapter;
return adapter.SelectCommand.BeginExecuteReader(callback, state);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}

protected override OutputResult EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
{
try
{
SqlDataAdapter adapter = (SqlDataAdapter)context.UserState;
//Create Output Object
OutRes = new OutputResult();
// End Execute Reader
reader = adapter.SelectCommand.EndExecuteReader(result);
reader.Close();
// Create DataSet
dataSet = new DataSet();
adapter.Fill(dataSet);
// Count Rows and Columns
int countRows = dataSet.Tables[0].Rows.Count;
int countField = dataSet.Tables[0].Columns.Count;

//countRows = 105; //debugging

// Create Word document
word = new Word.Application();
word.Visible = false;
document = word.Documents.Add();
word.Selection.TypeText("SQL: " + cmdSQL);
document.Tables.Add(word.Selection.Range, countRows, countField,
Word.WdDefaultTableBehavior.wdWord9TableBehavior,
Word.WdAutoFitBehavior.wdAutoFitContent);
for (int itemRow = 1; itemRow <= countRows; itemRow++)
{
for (int itemColumn = 1; itemColumn <= countField; itemColumn++)
{
word.ActiveDocument.Tables[1].Cell(itemRow, itemColumn).Range.InsertAfter(dataSet.Tables[0].Rows[itemRow - 1].ItemArray[itemColumn - 1].ToString());
}
}

// Output text in end Page
word.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast, ref missing, ref missing);
word.Selection.TypeParagraph();
word.Selection.TypeText("All in Table: " + countRows.ToString() + " rows");

// Write File in directory Activity
activityDirectory = AppDomain.CurrentDomain.BaseDirectory;
document.SaveAs(activityDirectory + nameFile);

// Close All
document.Close(ref missing, ref missing, ref missing);
word.Quit(ref missing, ref missing, ref missing);
document = null;
word = null;

// Read File in byte[]
OutRes.byteDocx = File.ReadAllBytes(activityDirectory + nameFile);
OutRes.dataSet = this.dataSet;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
this.OutRes.Error = ex.ToString();
}
finally
{
if (document != null)
{
document.Close(ref missing, ref missing, ref missing);
document = null;
}

if (word != null)
{
word.Quit(ref missing, ref missing, ref missing);
word = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
this.connect.Close();
}
return OutRes;
}
}
}
4. Activity DbToDocx
, , DataGridView Microsofr Word.
code
> /* 27.09.2012
Activity DbToDataSet.
1. InputArguments.
2. OutputResult DataSet Docx
byte[].
3. tempory.docx
4. tempory.docx
5. 堠 DataGridView.
: DataSet docx .
*/
using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ClassLibrary;
using System.Diagnostics;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.IO;

namespace WindowsFormsClient
{
public partial class Form1 : Form
{
private ServiceReference.ServiceClient client;
private ClassLibrary.InputArguments input;
private ClassLibrary.OutputResult output;

private Word._Application word = null;
private Word._Document document = null;
Object missing = (Object)Type.Missing;

public Form1()
{
InitializeComponent();
client = new ServiceReference.ServiceClient();
input = new InputArguments();
output = new OutputResult();
textBox2.Text = @"Data Source=PROGR\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True; Asynchronous Processing=true";
textBox3.Text = @"SELECT Orders.OrderID, Orders.CustomerID, Orders.ShipAddress FROM Orders";
textBox1.Text = "DbToDocx.docx";
}


private void button1_Click(object sender, EventArgs e)
{
try
{
// Input string for connect
input.ConnectStrig = textBox2.Text;
// Input SQL command
input.SqlCommand = textBox3.Text;
// Input name File for write
input.nameFile = textBox1.Text;
// Set null view
this.dataGridView1.DataSource = null;
this.dataGridView1.Update();

// Send and receive data
output = client.GetData(input);

// Write docx File in Disk
File.WriteAllBytes(@"g:\tempory.docx", output.byteDocx);
// Open docx File
word = new Word.Application();
document = word.Documents.Open(@"g:\tempory.docx");
word.Visible = true;

// View data
this.dataGridView1.DataSource = output.dataSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}

}

Visual Studio 2010 : Activity, Workflow Activity, Windows Form .
. C , .
27.09.2012 .
© Argument Ltd, 2017 ,  : 8-921-215-45-70,   e-mail