Saturday, 03 July 2010 00:50
EXCEL Data Import in C#, VB.NET
The sample demonstrates how to import the data from datatable to spreadsheet.
private void btnRun_Click(object sender, System.EventArgs e) { Workbook workbook = new Workbook(); //Initialize worksheet Worksheet sheet = workbook.Worksheets[0]; sheet.InsertDataTable((DataTable)this.dataGrid1.DataSource,true,2,1,-1,-1); //Sets body style CellStyle oddStyle = workbook.Styles.Add("oddStyle"); oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; oddStyle.KnownColor = ExcelColors.LightGreen1; CellStyle evenStyle = workbook.Styles.Add("evenStyle"); evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; evenStyle.KnownColor = ExcelColors.LightTurquoise; foreach( CellRange range in sheet.AllocatedRange.Rows) { if (range.Row % 2 == 0) range.CellStyleName = evenStyle.Name; else range.CellStyleName = oddStyle.Name; } //Sets header style CellStyle styleHeader = sheet.Rows[0].Style; styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; styleHeader.VerticalAlignment = VerticalAlignType.Center; styleHeader.KnownColor = ExcelColors.Green; styleHeader.Font.KnownColor = ExcelColors.White; styleHeader.Font.IsBold = true; sheet.Columns[sheet.AllocatedRange.LastColumn - 1].Style.NumberFormat = "\"$\"#,##0"; sheet.Columns[sheet.AllocatedRange.LastColumn - 2].Style.NumberFormat = "\"$\"#,##0"; sheet.AllocatedRange.AutoFitColumns(); sheet.AllocatedRange.AutoFitRows(); sheet.Rows[0].RowHeight = 20; workbook.SaveToFile("sample.xls"); ExcelDocViewer( workbook.FileName ); } private void Form1_Load(object sender, System.EventArgs e) { Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\..\..\..\..\Data\DataTableSample.xls"); //Initialize worksheet Worksheet sheet = workbook.Worksheets[0]; this.dataGrid1.DataSource = sheet.ExportDataTable(); } private void ExcelDocViewer( string fileName ) { try { System.Diagnostics.Process.Start(fileName); } catch{} }
Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click Dim workbook As Workbook = New Workbook() 'Initialize worksheet Dim sheet As Worksheet = workbook.Worksheets(0) sheet.InsertDataTable(CType(Me.dataGrid1.DataSource, DataTable),True,2,1,-1,-1) 'Sets body style Dim oddStyle As CellStyle = workbook.Styles.Add("oddStyle") oddStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin oddStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin oddStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin oddStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin oddStyle.KnownColor = ExcelColors.LightGreen1 Dim evenStyle As CellStyle = workbook.Styles.Add("evenStyle") evenStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin evenStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin evenStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin evenStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin evenStyle.KnownColor = ExcelColors.LightTurquoise For Each range As CellRange In sheet.AllocatedRange.Rows If range.Row Mod 2 = 0 Then range.CellStyleName = evenStyle.Name Else range.CellStyleName = oddStyle.Name End If Next range 'Sets header style Dim styleHeader As CellStyle = sheet.Rows(0).Style styleHeader.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin styleHeader.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin styleHeader.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin styleHeader.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin styleHeader.VerticalAlignment = VerticalAlignType.Center styleHeader.KnownColor = ExcelColors.Green styleHeader.Font.KnownColor = ExcelColors.White styleHeader.Font.IsBold = True sheet.Columns(sheet.AllocatedRange.LastColumn - 1).Style.NumberFormat = """$""#,##0" sheet.Columns(sheet.AllocatedRange.LastColumn - 2).Style.NumberFormat = """$""#,##0" sheet.AllocatedRange.AutoFitColumns() sheet.AllocatedRange.AutoFitRows() sheet.Rows(0).RowHeight = 20 workbook.SaveToFile("sample.xls") ExcelDocViewer(workbook.FileName) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim workbook As Workbook = New Workbook() workbook.LoadFromFile("..\..\..\..\..\..\Data\DataTableSample.xls") 'Initailize worksheet Dim sheet As Worksheet = workbook.Worksheets(0) Me.dataGrid1.DataSource = sheet.ExportDataTable() End Sub Private Sub ExcelDocViewer(ByVal fileName As String) Try System.Diagnostics.Process.Start(fileName) Catch End Try End Sub
Published in
DataTable