Word document setup in C#, VB.NET
The sample demonstrates how to set document properties.
//Create word document Document document = new Document(); document.BuiltinDocumentProperties.Title = "Document Demo Document"; document.BuiltinDocumentProperties.Subject = "demo"; document.BuiltinDocumentProperties.Author = "James"; document.BuiltinDocumentProperties.Company = "e-iceblue"; document.BuiltinDocumentProperties.Manager = "Jakson"; document.BuiltinDocumentProperties.Category = "Doc Demos"; document.BuiltinDocumentProperties.Keywords = "Document, Property, Demo"; document.BuiltinDocumentProperties.Comments = "This document is just a demo."; Section section = document.AddSection(); section.PageSetup.Margins.Top = 72f; section.PageSetup.Margins.Bottom = 72f; section.PageSetup.Margins.Left = 89.85f; section.PageSetup.Margins.Right = 89.85f; String p1 = "Microsoft Word is a word processor designed by Microsoft. " + "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. " + "Subsequent versions were later written for several other platforms including " + "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), " + "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). "; String p2 = "Microsoft Office Word instead of merely Microsoft Word. " + "The 2010 version appears to be branded as Microsoft Word, " + "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac."; section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14; section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14; //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc);
'Create word document Dim document As New Document() document.BuiltinDocumentProperties.Title = "Document Demo Document" document.BuiltinDocumentProperties.Subject = "demo" document.BuiltinDocumentProperties.Author = "James" document.BuiltinDocumentProperties.Company = "e-iceblue" document.BuiltinDocumentProperties.Manager = "Jakson" document.BuiltinDocumentProperties.Category = "Doc Demos" document.BuiltinDocumentProperties.Keywords = "Document, Property, Demo" document.BuiltinDocumentProperties.Comments = "This document is just a demo." Dim section As Section = document.AddSection() section.PageSetup.Margins.Top = 72.0F section.PageSetup.Margins.Bottom = 72.0F section.PageSetup.Margins.Left = 89.85F section.PageSetup.Margins.Right = 89.85F Dim p1 As String _ = "Microsoft Word is a word processor designed by Microsoft. " _ + "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. " _ + "Subsequent versions were later written for several other platforms including " _ + "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), " _ + "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). " Dim p2 As String _ = "Microsoft Office Word instead of merely Microsoft Word. " _ + "The 2010 version appears to be branded as Microsoft Word, " _ + "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac." section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14 section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14 'Save doc file. document.SaveToFile("Sample.doc", FileFormat.Doc)
Word merge event handler in C#, VB.NET
The sample demonstrates how to handle merge event.
public partial class Form1 : Form { private int lastIndex = 0; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Create word document Document document = new Document(); document.LoadFromFile(@"..\..\..\..\..\..\Data\Fax2.doc"); lastIndex = 0; List<CustomerRecord> customerRecords = new List<CustomerRecord>(); CustomerRecord c1 = new CustomerRecord(); c1.ContactName = "Lucy"; c1.Fax = "786-324-10"; c1.Date = DateTime.Now; customerRecords.Add(c1); CustomerRecord c2 = new CustomerRecord(); c2.ContactName = "Lily"; c2.Fax = "779-138-13"; c2.Date = DateTime.Now; customerRecords.Add(c2); CustomerRecord c3 = new CustomerRecord(); c3.ContactName = "James"; c3.Fax = "363-287-02"; c3.Date = DateTime.Now; customerRecords.Add(c3); //Execute mailmerge document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField); document.MailMerge.ExecuteGroup(new MailMergeDataTable("Customer", customerRecords)); //Save doc file. document.SaveToFile(@"Sample.doc", FileFormat.Doc); } void MailMerge_MergeField(object sender, MergeFieldEventArgs args) { //Next row if (args.RowIndex > lastIndex) { lastIndex = args.RowIndex; AddPageBreakForMergeField(args.CurrentMergeField); } } void AddPageBreakForMergeField(IMergeField mergeField) { //Find position of needing to add page break bool foundGroupStart = false; Paragraph paramgraph = mergeField.PreviousSibling.Owner as Paragraph; MergeField merageField = null; while (!foundGroupStart) { paramgraph = paramgraph.PreviousSibling as Paragraph; for (int i = 0; i < paramgraph.Items.Count; i++) { merageField = paramgraph.Items[i] as MergeField; if ((merageField != null) && (merageField.Prefix == "GroupStart")) { foundGroupStart = true; break; } } } paramgraph.AppendBreak(BreakType.PageBreak); } } public class CustomerRecord { private string m_contactName; public string ContactName { get { return m_contactName; } set { m_contactName = value; } } private string m_fax; public string Fax { get { return m_fax; } set { m_fax = value; } } private DateTime m_date; public DateTime Date { get { return m_date; } set { m_date = value; } } }
Public Partial Class Form1 Inherits Form Private lastIndex As Integer = 0 Public Sub New() InitializeComponent() End Sub Private Sub button1_Click(sender As Object, e As EventArgs) 'Create word document Dim document As New Document() document.LoadFromFile("..\..\..\..\..\Data\Fax2.doc") lastIndex = 0 Dim customerRecords As New List(Of CustomerRecord)() Dim c1 As New CustomerRecord() c1.ContactName = "Lucy" c1.Fax = "786-324-10" c1.[Date] = DateTime.Now customerRecords.Add(c1) Dim c2 As New CustomerRecord() c2.ContactName = "Lily" c2.Fax = "779-138-13" c2.[Date] = DateTime.Now customerRecords.Add(c2) Dim c3 As New CustomerRecord() c3.ContactName = "James" c3.Fax = "363-287-02" c3.[Date] = DateTime.Now customerRecords.Add(c3) 'Execute mailmerge document.MailMerge.MergeField += New MergeFieldEventHandler(AddressOf MailMerge_MergeField) document.MailMerge.ExecuteGroup(New MailMergeDataTable("Customer", customerRecords)) 'Save doc file. document.SaveToFile("Sample.doc", FileFormat.Doc) End Sub Private Sub MailMerge_MergeField(sender As Object, args As MergeFieldEventArgs) 'Next row If args.RowIndex > lastIndex Then lastIndex = args.RowIndex AddPageBreakForMergeField(args.CurrentMergeField) End If End Sub Private Sub AddPageBreakForMergeField(mergeField As IMergeField) 'Find position of needing to add page break Dim foundGroupStart As Boolean = False Dim paramgraph As Paragraph = TryCast(mergeField.PreviousSibling.Owner, Paragraph) Dim merageField As MergeField = Nothing While Not foundGroupStart paramgraph = TryCast(paramgraph.PreviousSibling, Paragraph) For i As Integer = 0 To paramgraph.Items.Count - 1 merageField = TryCast(paramgraph.Items(i), MergeField) If (merageField IsNot Nothing) AndAlso (merageField.Prefix = "GroupStart") Then foundGroupStart = True Exit For End If Next End While paramgraph.AppendBreak(BreakType.PageBreak) End Sub End Class Public Class CustomerRecord Private m_contactName As String Public Property ContactName() As String Get Return m_contactName End Get Set m_contactName = value End Set End Property Private m_fax As String Public Property Fax() As String Get Return m_fax End Get Set m_fax = value End Set End Property Private m_date As DateTime Public Property [Date]() As DateTime Get Return m_date End Get Set m_date = value End Set End Property End Class End Namespace
Word to xml in C#, VB.NET
The sample demonstrates how to export doc document to XML file.
//Create word document Document document = new Document(); Section section = document.AddSection(); String[] header = { "Name", "Capital", "Continent", "Area", "Population" }; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Chile", "Santiago", "South America", "756943", "13200000"}, new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"}, new String[]{"Cuba", "Havana", "North America", "114524", "10600000"}, new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"}, new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"}, new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"}, new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"}, new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"}, new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, new String[]{"Peru", "Lima", "South America", "1285215", "21600000"}, new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"}, new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"}, new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"} }; Spire.Doc.Table table = section.AddTable(); table.ResetCells(data.Length + 1, header.Length); // ***************** First Row ************************* TableRow row = table.Rows[0]; row.IsHeader = true; row.Height = 20; //unit: point, 1point = 0.3528 mm row.HeightType = TableRowHeightType.Exactly; row.RowFormat.BackColor = Color.Gray; for (int i = 0; i < header.Length; i++) { row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = row.Cells[i].AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange txtRange = p.AppendText(header[i]); txtRange.CharacterFormat.Bold = true; } for (int r = 0; r < data.Length; r++) { TableRow dataRow = table.Rows[r + 1]; dataRow.Height = 20; dataRow.HeightType = TableRowHeightType.Exactly; dataRow.RowFormat.BackColor = Color.Empty; for (int c = 0; c < data[r].Length; c++) { dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; dataRow.Cells[c].AddParagraph().AppendText(data[r][c]); } } //Save xml file. document.SaveToFile("Sample.xml",FileFormat.Xml);
'Create word document Dim document_Renamed As New Document() Dim section As Section = document_Renamed.AddSection() Dim header As String() = {"Name", "Capital", "Continent", "Area", "Population"} Dim data As String()() = { _ New String() {"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, _ New String() {"Bolivia", "La Paz", "South America", "1098575", "7300000"}, _ New String() {"Brazil", "Brasilia", "South America", "8511196", "150400000"}, _ New String() {"Canada", "Ottawa", "North America", "9976147", "26500000"}, _ New String() {"Chile", "Santiago", "South America", "756943", "13200000"}, _ New String() {"Colombia", "Bagota", "South America", "1138907", "33000000"}, _ New String() {"Cuba", "Havana", "North America", "114524", "10600000"}, _ New String() {"Ecuador", "Quito", "South America", "455502", "10600000"}, _ New String() {"El Salvador", "San Salvador", "North America", "20865", "5300000"}, _ New String() {"Guyana", "Georgetown", "South America", "214969", "800000"}, _ New String() {"Jamaica", "Kingston", "North America", "11424", "2500000"}, _ New String() {"Mexico", "Mexico City", "North America", "1967180", "88600000"}, _ New String() {"Nicaragua", "Managua", "North America", "139000", "3900000"}, _ New String() {"Paraguay", "Asuncion", "South America", "406576", "4660000"}, _ New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _ New String() {"United States of America", "Washington", "North America", "9363130", "249200000"}, _ New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _ New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _ } Dim table As Spire.Doc.Table = section.AddTable() table.ResetCells(data.Length + 1, header.Length) ' ***************** First Row ************************* Dim row As TableRow = table.Rows(0) row.IsHeader = True row.Height = 20 'unit: point, 1point = 0.3528 mm row.HeightType = TableRowHeightType.Exactly row.RowFormat.BackColor = Color.Gray For i As Integer = 0 To header.Length - 1 row.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle Dim p As Paragraph = row.Cells(i).AddParagraph() p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center Dim txtRange As TextRange = p.AppendText(header(i)) txtRange.CharacterFormat.Bold = True Next For r As Integer = 0 To data.Length - 1 Dim dataRow As TableRow = table.Rows(r + 1) dataRow.Height = 20 dataRow.HeightType = TableRowHeightType.Exactly dataRow.RowFormat.BackColor = Color.Empty For c As Integer = 0 To data(r).Length - 1 dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle dataRow.Cells(c).AddParagraph().AppendText(data(r)(c)) Next Next 'Save xml file. document_Renamed.SaveToFile("Sample.xml", FileFormat.Xml)
Word table in C#, VB.NET
The sample demonstrates how to create table in word document.
//Create word document Document document = new Document(); Section section = document.AddSection(); String[] header = { "Name", "Capital", "Continent", "Area", "Population" }; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Chile", "Santiago", "South America", "756943", "13200000"}, new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"}, new String[]{"Cuba", "Havana", "North America", "114524", "10600000"}, new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"}, new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"}, new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"}, new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"}, new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"}, new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, new String[]{"Peru", "Lima", "South America", "1285215", "21600000"}, new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"}, new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"}, new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"} }; Spire.Doc.Table table = section.AddTable(); table.ResetCells(data.Length + 1, header.Length); // ***************** First Row ************************* TableRow row = table.Rows[0]; row.IsHeader = true; row.Height = 20; //unit: point, 1point = 0.3528 mm row.HeightType = TableRowHeightType.Exactly; row.RowFormat.BackColor = Color.Gray; for (int i = 0; i < header.Length; i++) { row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = row.Cells[i].AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange txtRange = p.AppendText(header[i]); txtRange.CharacterFormat.Bold = true; } for (int r = 0; r < data.Length; r++) { TableRow dataRow = table.Rows[r + 1]; dataRow.Height = 20; dataRow.HeightType = TableRowHeightType.Exactly; dataRow.RowFormat.BackColor = Color.Empty; for (int c = 0; c < data[r].Length; c++) { dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; dataRow.Cells[c].AddParagraph().AppendText(data[r][c]); } } //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc);
'Create word document Dim document_Renamed As New Document() Dim section As Section = document_Renamed.AddSection() Dim header As String() = {"Name", "Capital", "Continent", "Area", "Population"} Dim data As String()() = { _ New String() {"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, _ New String() {"Bolivia", "La Paz", "South America", "1098575", "7300000"}, _ New String() {"Brazil", "Brasilia", "South America", "8511196", "150400000"}, _ New String() {"Canada", "Ottawa", "North America", "9976147", "26500000"}, _ New String() {"Chile", "Santiago", "South America", "756943", "13200000"}, _ New String() {"Colombia", "Bagota", "South America", "1138907", "33000000"}, _ New String() {"Cuba", "Havana", "North America", "114524", "10600000"}, _ New String() {"Ecuador", "Quito", "South America", "455502", "10600000"}, _ New String() {"El Salvador", "San Salvador", "North America", "20865", "5300000"}, _ New String() {"Guyana", "Georgetown", "South America", "214969", "800000"}, _ New String() {"Jamaica", "Kingston", "North America", "11424", "2500000"}, _ New String() {"Mexico", "Mexico City", "North America", "1967180", "88600000"}, _ New String() {"Nicaragua", "Managua", "North America", "139000", "3900000"}, _ New String() {"Paraguay", "Asuncion", "South America", "406576", "4660000"}, _ New String() {"Peru", "Lima", "South America", "1285215", "21600000"}, _ New String() {"United States of America", "Washington", "North America", "9363130", "249200000"}, _ New String() {"Uruguay", "Montevideo", "South America", "176140", "3002000"}, _ New String() {"Venezuela", "Caracas", "South America", "912047", "19700000"} _ } Dim table As Spire.Doc.Table = section.AddTable() table.ResetCells(data.Length + 1, header.Length) ' ***************** First Row ************************* Dim row As TableRow = table.Rows(0) row.IsHeader = True row.Height = 20 'unit: point, 1point = 0.3528 mm row.HeightType = TableRowHeightType.Exactly row.RowFormat.BackColor = Color.Gray For i As Integer = 0 To header.Length - 1 row.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle Dim p As Paragraph = row.Cells(i).AddParagraph() p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center Dim txtRange As TextRange = p.AppendText(header(i)) txtRange.CharacterFormat.Bold = True Next For r As Integer = 0 To data.Length - 1 Dim dataRow As TableRow = table.Rows(r + 1) dataRow.Height = 20 dataRow.HeightType = TableRowHeightType.Exactly dataRow.RowFormat.BackColor = Color.Empty For c As Integer = 0 To data(r).Length - 1 dataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle dataRow.Cells(c).AddParagraph().AppendText(data(r)(c)) Next Next 'Save doc file. document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
Word indent in C#, VB.NET
The sample demonstrates how to indent paragraph.
//Create word document Document document = new Document(); //Create a new secition Section section = document.AddSection(); //Create a new paragraph Paragraph paragraph = section.AddParagraph(); //Append Text paragraph.AppendText("Using items list to show Indent demo."); paragraph.ApplyStyle(BuiltinStyle.Heading3); paragraph = section.AddParagraph(); for (int i = 0; i < 10; i++) { paragraph = section.AddParagraph(); paragraph.AppendText(String.Format("Indent Demo Node{0}", i)); if(i == 0) { paragraph.ListFormat.ApplyBulletStyle(); } else { paragraph.ListFormat.ContinueListNumbering(); } paragraph.ListFormat.CurrentListLevel.NumberPosition = -10; } //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc);
'Create word document Dim document_Renamed As New Document() 'Create a new secition Dim section_Renamed As Section = document_Renamed.AddSection() 'Create a new paragraph Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph() 'Append Text paragraph_Renamed.AppendText("Using items list to show Indent demo.") paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading3) paragraph_Renamed = section_Renamed.AddParagraph() For i As Integer = 0 To 9 paragraph_Renamed = section_Renamed.AddParagraph() Dim text As String _ = "Indent Demo Node" & i.ToString() Dim txtRange As TextRange = paragraph_Renamed.AppendText(text) If i = 0 Then paragraph_Renamed.ListFormat.ApplyBulletStyle() Else paragraph_Renamed.ListFormat.ContinueListNumbering() End If paragraph_Renamed.ListFormat.CurrentListLevel.NumberPosition = -10 Next i 'Save doc file. document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
Word font and color in C#, VB.NET
The sample demonstrates how to set font and color.
//Create word document Document document = new Document(); //Create a new secition Section section = document.AddSection(); //Create a new paragraph Paragraph paragraph = section.AddParagraph(); //Append Text String text = "This paragraph is demo of text font and color. " + "The font name of this paragraph is Tahoma. " + "The font size of this paragraph is 20. " + "The under line style of this paragraph is DotDot. " + "The color of this paragraph is Blue. "; TextRange txtRange = paragraph.AppendText(text); //Font name txtRange.CharacterFormat.FontName = "Tahoma"; //Font size txtRange.CharacterFormat.FontSize = 20; //Underline txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot; //Change text color txtRange.CharacterFormat.TextColor = Color.Blue; //Save doc file. document.SaveToFile("Sample.doc",FileFormat.Doc);
'Create word document Dim document_Renamed As New Document() 'Create a new secition Dim section_Renamed As Section = document_Renamed.AddSection() 'Create a new paragraph Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph() 'Append Text Dim text As String _ = "This paragraph is demo of text font and color. " _ & "The font name of this paragraph is Tahoma. " _ & "The font size of this paragraph is 20. " _ & "The under line style of this paragraph is DotDot. " _ & "The color of this paragraph is Blue. " Dim txtRange As TextRange = paragraph_Renamed.AppendText(text) 'Font name txtRange.CharacterFormat.FontName = "Tahoma" 'Font size txtRange.CharacterFormat.FontSize = 20 'Underline txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot 'Change text color txtRange.CharacterFormat.TextColor = Color.Blue 'Save doc file. document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
Data Export Text/SYLK/CSV/DIF in C#, VB.NET
This sample demonstrates how to export data table to text/sylk/csv/dif file.
The picture above represents using Excel application to open a SYLK type output file.
private void btnText_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"; System.Data.OleDb.OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); oleDbCommand1.CommandText = "select * from parts"; oleDbCommand1.Connection = oleDbConnection1; Spire.DataExport.TXT.TXTExport txtExport1 = new Spire.DataExport.TXT.TXTExport(); txtExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; txtExport1.DataFormats.CultureName = "zh-CN"; txtExport1.DataFormats.Currency = "c"; txtExport1.DataFormats.DateTime = "yyyy-M-d H:mm"; txtExport1.DataFormats.Float = "g"; txtExport1.DataFormats.Integer = "g"; txtExport1.DataFormats.Time = "H:mm"; txtExport1.DataEncoding = Spire.DataExport.Common.EncodingType.ASCII; txtExport1.SQLCommand = oleDbCommand1; oleDbConnection1.Open(); txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.SYLK; txtExport1.FileName = "sample.slk"; txtExport1.SaveToFile(); txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.CSV; txtExport1.FileName = "sample.csv"; txtExport1.SaveToFile(); txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.DIF; txtExport1.FileName = "sample.dif"; txtExport1.SaveToFile(); txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.TXT; txtExport1.FileName = "sample.txt"; txtExport1.SaveToFile(); }
Private Sub btnText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnText.Click Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection() oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb" Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand() oleDbCommand1.CommandText = "select * from parts" oleDbCommand1.Connection = oleDbConnection1 Dim txtExport1 As New Spire.DataExport.TXT.TXTExport() txtExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView txtExport1.DataFormats.CultureName = "zh-CN" txtExport1.DataFormats.Currency = "c" txtExport1.DataFormats.DateTime = "yyyy-M-d H:mm" txtExport1.DataFormats.Float = "g" txtExport1.DataFormats.[Integer] = "g" txtExport1.DataFormats.Time = "H:mm" txtExport1.DataEncoding = Spire.DataExport.Common.EncodingType.ASCII txtExport1.SQLCommand = oleDbCommand1 oleDbConnection1.Open() txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.SYLK txtExport1.FileName = "sample.slk" txtExport1.SaveToFile() txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.CSV txtExport1.FileName = "sample.csv" txtExport1.SaveToFile() txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.DIF txtExport1.FileName = "sample.dif" txtExport1.SaveToFile() txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.TXT txtExport1.FileName = "sample.txt" txtExport1.SaveToFile() End Sub
Data Export SQL Script in C#, VB.NET
This sample demonstrates how to export data table to sql script.
private void btnSQLScript_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"; System.Data.OleDb.OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); oleDbCommand1.CommandText = "select * from parts"; oleDbCommand1.Connection = oleDbConnection1; Spire.DataExport.SQL.SQLExport sqlExport1 = new Spire.DataExport.SQL.SQLExport(); sqlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; sqlExport1.CommitStatement = "COMMIT;"; sqlExport1.DataFormats.CultureName = "zh-CN"; sqlExport1.DataFormats.Currency = "c"; sqlExport1.DataFormats.DateTime = "yyyy-M-d H:mm"; sqlExport1.DataFormats.Float = "g"; sqlExport1.DataFormats.Integer = "g"; sqlExport1.DataFormats.Time = "H:mm"; sqlExport1.FileName = "sample.sql"; sqlExport1.SQLCommand = oleDbCommand1; sqlExport1.TableName = "TestTable"; oleDbConnection1.Open(); sqlExport1.SaveToFile(); }
Private Sub btnSQLScript_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLScript.Click Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection() oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb" Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand() oleDbCommand1.CommandText = "select * from parts" oleDbCommand1.Connection = oleDbConnection1 Dim sqlExport1 As New Spire.DataExport.SQL.SQLExport() sqlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView sqlExport1.CommitStatement = "COMMIT;" sqlExport1.DataFormats.CultureName = "zh-CN" sqlExport1.DataFormats.Currency = "c" sqlExport1.DataFormats.DateTime = "yyyy-M-d H:mm" sqlExport1.DataFormats.Float = "g" sqlExport1.DataFormats.[Integer] = "g" sqlExport1.DataFormats.Time = "H:mm" sqlExport1.FileName = "sample.sql" sqlExport1.SQLCommand = oleDbCommand1 sqlExport1.TableName = "TestTable" oleDbConnection1.Open() sqlExport1.SaveToFile() End Sub
Data Export XML in C#, VB.NET
This sample demonstrates how to export data table into xml file.
private void btnXML_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"; System.Data.OleDb.OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); oleDbCommand1.CommandText = "select * from parts"; oleDbCommand1.Connection = oleDbConnection1; Spire.DataExport.XML.XMLExport xmlExport1 = new Spire.DataExport.XML.XMLExport(); xmlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; xmlExport1.DataFormats.CultureName = "zh-CN"; xmlExport1.DataFormats.Currency = "c"; xmlExport1.DataFormats.DateTime = "yyyy-M-d H:mm"; xmlExport1.DataFormats.Float = "g"; xmlExport1.DataFormats.Integer = "g"; xmlExport1.DataFormats.Time = "H:mm"; xmlExport1.FileName = "sample.xml"; xmlExport1.SQLCommand = oleDbCommand1; oleDbConnection1.Open(); xmlExport1.SaveToFile(); }
Private Sub btnXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXML.Click Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection() oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb" Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand() oleDbCommand1.CommandText = "select * from parts" oleDbCommand1.Connection = oleDbConnection1 Dim xmlExport1 As New Spire.DataExport.XML.XMLExport() xmlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView xmlExport1.DataFormats.CultureName = "zh-CN" xmlExport1.DataFormats.Currency = "c" xmlExport1.DataFormats.DateTime = "yyyy-M-d H:mm" xmlExport1.DataFormats.Float = "g" xmlExport1.DataFormats.[Integer] = "g" xmlExport1.DataFormats.Time = "H:mm" xmlExport1.FileName = "sample.xml" xmlExport1.SQLCommand = oleDbCommand1 oleDbConnection1.Open() xmlExport1.SaveToFile() End Sub
Data Export MS Access in C#, VB.NET
This sample demonstrates how to export data table into ms-access file.
The picture above represents using Excel application to open the result ms-access file.
private void btnMSAccess_Click(object sender, EventArgs e) { System.Data.OleDb.OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"; System.Data.OleDb.OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); oleDbCommand1.CommandText = "select * from parts"; oleDbCommand1.Connection = oleDbConnection1; Spire.DataExport.Access.AccessExport accessExport1 = new Spire.DataExport.Access.AccessExport(); accessExport1.DatabaseName = "test.mdb"; accessExport1.DataFormats.CultureName = "zh-CN"; accessExport1.DataFormats.Currency = "c"; accessExport1.DataFormats.DateTime = "yyyy-M-d H:mm"; accessExport1.DataFormats.Float = "g"; accessExport1.DataFormats.Integer = "g"; accessExport1.DataFormats.Time = "H:mm"; accessExport1.SQLCommand = oleDbCommand1; accessExport1.TableName = "ExportData"; oleDbConnection1.Open(); accessExport1.SaveToFile(); }
Private Sub btnMSAccess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSAccess.Click Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection() oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb" Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand() oleDbCommand1.CommandText = "select * from parts" oleDbCommand1.Connection = oleDbConnection1 Dim accessExport1 As New Spire.DataExport.Access.AccessExport() accessExport1.DatabaseName = "test.mdb" accessExport1.DataFormats.CultureName = "zh-CN" accessExport1.DataFormats.Currency = "c" accessExport1.DataFormats.DateTime = "yyyy-M-d H:mm" accessExport1.DataFormats.Float = "g" accessExport1.DataFormats.[Integer] = "g" accessExport1.DataFormats.Time = "H:mm" accessExport1.SQLCommand = oleDbCommand1 accessExport1.TableName = "ExportData" oleDbConnection1.Open() accessExport1.SaveToFile() End Sub