Convert text file to xml file which has delimiter as tab.
char strDelimiter = '\t';
string strFilePath = "c:/Test.txt";
DataSet oDS = new DataSet();
DataTable oTable = new DataTable();
DataRow oRows;
int intCounter = 0;
oDS.DataSetName = "Data_Root";
oDS.Namespace = strFilePath;
oDS.Tables.Add("Table1");
StreamReader oSR = new StreamReader(strFilePath);
//Go to the top of the file
oSR.BaseStream.Seek(0, SeekOrigin.Begin);
//Add in the Header Columns
foreach (string strFields1 in oSR.ReadLine().Split(strDelimiter))
oDS.Tables[0].Columns.Add(strFields1);
//Now add in the Rows
oTable = oDS.Tables[0];
while (oSR.Peek() > -1)
{
oRows = oTable.NewRow();
foreach (string strFields2 in oSR.ReadLine().Split(strDelimiter))
{
oRows[intCounter] = strFields2;
intCounter = intCounter + 1;
}
intCounter = 0;
oTable.Rows.Add(oRows);
}
//Create XML file
oDS.WriteXml("c:/Text_To_XML.xml");
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Wednesday, February 3, 2010
Read Tables Name from Access DataBase
OleDbConnection con;
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
con.Open();
DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null, null, null, "TABLE"});
for (int y = 0; y < schemaTable.Rows.Count; y++)
listBox1.Items.Add(schemaTable.Rows[y][2].ToString());
con.Close();
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
con.Open();
DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null, null, null, "TABLE"});
for (int y = 0; y < schemaTable.Rows.Count; y++)
listBox1.Items.Add(schemaTable.Rows[y][2].ToString());
con.Close();
Read column names from Access database table
OleDbConnection con;
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
con.Open();
cmd = new OleDbCommand("SELECT * from Table1", con);
reader = cmd.ExecuteReader();
int iColumns = reader.VisibleFieldCount;
for (int x = 0; x < iColumns; x++)
{
listBox1.Items.Add(reader.GetName(x));
}
con.Close();
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
con.Open();
cmd = new OleDbCommand("SELECT * from Table1", con);
reader = cmd.ExecuteReader();
int iColumns = reader.VisibleFieldCount;
for (int x = 0; x < iColumns; x++)
{
listBox1.Items.Add(reader.GetName(x));
}
con.Close();
Automation of Word Mail Merge using Windows Application
I have Template and database in C drive with name Template.doc and SampleDB.mdb respectively.
Add Reference: Microsoft Word 11.0 Object Library from COM tab
using Word = Microsoft.Office.Interop.Word;
Word.Application wrdApp;
Word._Document wrdDoc;
Object oTemplate = "c:\\Template.doc";
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oTrue = true;
Word.MailMerge wrdMailMerge;
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
// Create MailMerge Data.
wrdDoc = wrdApp.Documents.Open(ref oTemplate, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
wrdDoc.Select();
wrdMailMerge = wrdDoc.MailMerge;
object oQuery = "SELECT Name,Address1,City from Table1";
string strDB = "c:\\SampleDB.mdb";
wrdDoc.MailMerge.OpenDataSource(strDB, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
wrdMailMerge.SuppressBlankLines = true;
// Perform mail merge.
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref oFalse);
// Close the Template document.
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
// Release References.
wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;
Add Reference: Microsoft Word 11.0 Object Library from COM tab
using Word = Microsoft.Office.Interop.Word;
Word.Application wrdApp;
Word._Document wrdDoc;
Object oTemplate = "c:\\Template.doc";
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oTrue = true;
Word.MailMerge wrdMailMerge;
// Create an instance of Word and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;
// Create MailMerge Data.
wrdDoc = wrdApp.Documents.Open(ref oTemplate, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
wrdDoc.Select();
wrdMailMerge = wrdDoc.MailMerge;
object oQuery = "SELECT Name,Address1,City from Table1";
string strDB = "c:\\SampleDB.mdb";
wrdDoc.MailMerge.OpenDataSource(strDB, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
wrdMailMerge.SuppressBlankLines = true;
// Perform mail merge.
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref oFalse);
// Close the Template document.
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
// Release References.
wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;
Change BackColor for Listbox Items in Windows Application
Sample code to give alternate color for alternate items in listbox.
In my application lstBoxSelected is the name of listbox.
Write these two lines in page load event.
lstBoxSelected.DrawMode = DrawMode.OwnerDrawFixed;
lstBoxSelected.DrawItem += new DrawItemEventHandler(DrawListSelected);
DrawListSelected is the event for drawing backcolor for listitem.
private void DrawListSelected(object sender, DrawItemEventArgs e)
{
//Draw ListBox Items
Rectangle rect = new Rectangle();
rect = e.Bounds;
// Setup the stringformatting object
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
// Fill Backcolor for selected item
e.Graphics.FillRectangle(Brushes.Orange, e.Bounds);
}
else
{
Brush b; // Object used to define backcolor
b = Brushes.LightCyan;
if (e.Index % 2 == 0)
b = Brushes.PowderBlue;
else
b = Brushes.LightCyan;
e.Graphics.FillRectangle(b, rect);
}
if (lstBoxSelected.Items.Count > 0)
{
e.Graphics.DrawString(lstBoxSelected.Items[e.Index].ToString().Replace("*","").Trim(), e.Font, Brushes.Black, rect, sf);
}
}
In my application lstBoxSelected is the name of listbox.
Write these two lines in page load event.
lstBoxSelected.DrawMode = DrawMode.OwnerDrawFixed;
lstBoxSelected.DrawItem += new DrawItemEventHandler(DrawListSelected);
DrawListSelected is the event for drawing backcolor for listitem.
private void DrawListSelected(object sender, DrawItemEventArgs e)
{
//Draw ListBox Items
Rectangle rect = new Rectangle();
rect = e.Bounds;
// Setup the stringformatting object
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
// Fill Backcolor for selected item
e.Graphics.FillRectangle(Brushes.Orange, e.Bounds);
}
else
{
Brush b; // Object used to define backcolor
b = Brushes.LightCyan;
if (e.Index % 2 == 0)
b = Brushes.PowderBlue;
else
b = Brushes.LightCyan;
e.Graphics.FillRectangle(b, rect);
}
if (lstBoxSelected.Items.Count > 0)
{
e.Graphics.DrawString(lstBoxSelected.Items[e.Index].ToString().Replace("*","").Trim(), e.Font, Brushes.Black, rect, sf);
}
}
Thursday, January 28, 2010
Read or Write OLE File Property
Here are the steps to Read or Write the properties of files [Title, Subject, Author and Comments]
Download and install DsoFileSetup_KB224351_x86.exe from http://www.microsoft.com/downloads/details.aspx?FamilyId=9BA6FAC6-520B-4A0A-878A-53EC8300C4C2&displaylang=en
- Add reference to C:\DsoFile\dsofile.dll to your project (this is the default location)
private void Button1_Click(object sender, EventArgs e)
{
//This is the PDF file we want to update.
string strFilename = @"c:\Test.pdf";
//Create OleDocumentProperties object.
DSOFile.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(strFilename, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
dso.SummaryProperties.Title = "Test Title";
dso.SummaryProperties.Subject = "Test Subject";
dso.SummaryProperties.Author = "Test Author";
dso.SummaryProperties.Comments = "Test Comments";
dso.Save();
dso.Close(false);
}
{
//This is the PDF file we want to update.
string strFilename = @"c:\Test.pdf";
//Create OleDocumentProperties object.
DSOFile.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(strFilename, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
dso.SummaryProperties.Title = "Test Title";
dso.SummaryProperties.Subject = "Test Subject";
dso.SummaryProperties.Author = "Test Author";
dso.SummaryProperties.Comments = "Test Comments";
dso.Save();
dso.Close(false);
}
If you want run the exe of this application in other system, you have to register dsofile.dll.
For that copy dsofile.dll in that system and in command prompt navigate to the directory where the dsofile.dll is present and run the regsvr32 command like below.
regsvr32 dsofile.dll
Subscribe to:
Posts (Atom)
