Export CSV from a DataTable in a dataset

Export CSV from a DataTable in a dataset
Share it:

Export CSV from a DataTable in a dataset

XML is widely used for data communications between the applications, we  used CSV. Even now for when the new systems are being developed which must be communicated with these legacy applications, we've no choice if we cant choose anything aside from those systems are using. i'm going to develop a category which can be useful for exporting CSV.



Delimiter

CSV could also be a personality separated values format so we've to choose a personality as a delimiter. As delimiters are chosen supported various factors we'd like our class to be delimiters configurable.



Text Qualifiers

As there will be a risk if the delimiter is already present within the info , the text qualifiers are used to identify the text element’s boundary. Within this boundary if a delimiter character is present then the category won't consider it as a delimiter.



Configurable Column Headers

Some of the interfaces won't like headers, so make a switch to point out off the column header generations.



Generate the CSV string

 Once done add a delimiter character next thereto . If this is often often finished all rows then the string is ready to be written into the file system

Export CSV from a DataTable in a dataset


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
private void button5_Click(object sender, EventArgs e)
        {
            if (dgw.DataSource == null)
            {
                MessageBox.Show("Sorry nothing to export into excel sheet..", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            short rowsTotal;
            short colsTotal;
            short I;
            short j;
            short iC;
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            Excel.Application xlApp = new Excel.Application();
            try
            {
                Excel.Workbook excelBook = xlApp.Workbooks.Add();
                Excel.Worksheet excelWorksheet = (Excel.Worksheet)(excelBook.Worksheets[1]);
                xlApp.Visible = true;

                rowsTotal = System.Convert.ToInt16(dgw.RowCount);
                colsTotal = System.Convert.ToInt16(dgw.Columns.Count - 1);
                excelWorksheet.Cells.Select();
                excelWorksheet.Cells.Delete();
                for (iC = 0; iC <= colsTotal; iC++)
                {
                    excelWorksheet.Cells[1, iC + 1].Value = dgw.Columns[iC].HeaderText.ToString();
                }
                for (I = 0; I <= rowsTotal - 1; I++)
                {
                    for (j = 0; j <= colsTotal; j++)
                    {
                        excelWorksheet.Cells[I + 2, j + 1].value = dgw.Rows[I].Cells[j].Value.ToString();
                    }
                }
                excelWorksheet.Rows["1:1"].Font.FontStyle = "Bold";
                excelWorksheet.Rows["1:1"].Font.Size = 12;

                excelWorksheet.Cells.Columns.AutoFit();
                excelWorksheet.Cells.Select();
                excelWorksheet.Cells.EntireColumn.AutoFit();
                excelWorksheet.Cells[1, 1].Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //RELEASE ALLOACTED RESOURCES
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
                xlApp = null;
            }
        }
Share it:

dotnet

vbnet

Post A Comment:

0 comments: