Translate

Friday, 10 November 2017

generate csv file from c# datatable

Use this code :


Design

<div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </div>

css


using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;



   protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        DataTable dtCSV = new DataTable();
        dtCSV.Columns.AddRange(new DataColumn[] {
             new DataColumn("Emp_Code", typeof(string)),
             new DataColumn("Name", typeof(string)),
             new DataColumn("City", typeof(string)),
             new DataColumn("DOB", typeof(DateTime)),
             new DataColumn("Mobile", typeof(string)),
              new DataColumn("ctc", typeof(decimal)),
             new DataColumn("status", typeof(bool))
        });

        dtCSV.Rows.Add("1001", "PABITRA BEHERA", "Dhenkanal", "1990-04-28", "9000000001", 23.6, true);
        dtCSV.Rows.Add("1002", "TRUPTI RANJAN PANIGRAHI", "Bhadrak", "1990-05-29", "9000000002", 9.9, true);
        dtCSV.Rows.Add("1003", "GAYATRI PRASAD DAS", "Khordha","1990-06-30", "9000000003", 10.9, true);
        dtCSV.Rows.Add("1004", "Himansu sethi", "Bhubaneswr","1990-07-26", "9000000004", 11.9, true);

        using (StreamWriter writer = new StreamWriter("D:\\PABITRA\\Test_Pabitra_"+DateTime.Now.ToString("yyyyMMddhhmmssmm")+".csv"))
        {
            ConvertDataTableToCSV(dtCSV, writer, true);
        }
    }
    public static void ConvertDataTableToCSV(DataTable objDatatable, TextWriter objTextWriter, bool IsHeader) 
    {
        if (IsHeader)
        {
            IEnumerable<String> HeaderText = objDatatable.Columns
                .OfType<DataColumn>()
                .Select(column => ResetBlank(column.ColumnName));

            objTextWriter.WriteLine(String.Join(",", HeaderText));
        }

        IEnumerable<String> items = null;

        foreach (DataRow row in objDatatable.Rows)
        {
            items = row.ItemArray.Select(r => ResetBlank(r == null ? string.Empty :r.ToString()));
            objTextWriter.WriteLine(String.Join(",", items));
        }
        objTextWriter.Flush();
    }
    private static string ResetBlank(string value)
    {
        return String.Concat("\"",
        value.Replace("\"", "\"\""), "\"");
    }

------------------------------------------------------------------------------





All Code 
-------------------

Design
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </div>
    </form>
</body>

</html>



css

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
     
        DataTable dtCSV = new DataTable();
        dtCSV.Columns.AddRange(new DataColumn[] {
             new DataColumn("Emp_Code", typeof(string)),
             new DataColumn("Name", typeof(string)),
             new DataColumn("City", typeof(string)),
             new DataColumn("DOB", typeof(DateTime)),
             new DataColumn("Mobile", typeof(string)),
              new DataColumn("ctc", typeof(decimal)),
             new DataColumn("status", typeof(bool))
        });

        dtCSV.Rows.Add("1001", "PABITRA BEHERA", "Dhenkanal", "1990-04-28", "9000000001", 23.6, true);
        dtCSV.Rows.Add("1002", "TRUPTI RANJAN PANIGRAHI", "Bhadrak", "1990-05-29", "9000000002", 9.9, true);
        dtCSV.Rows.Add("1003", "GAYATRI PRASAD DAS", "Khordha","1990-06-30", "9000000003", 10.9, true);
        dtCSV.Rows.Add("1004", "Himansu sethi", "Bhubaneswr","1990-07-26", "9000000004", 11.9, true);

        using (StreamWriter writer = new StreamWriter("D:\\PABITRA\\Test_Pabitra_"+DateTime.Now.ToString("yyyyMMddhhmmssmm")+".csv"))
        {
            ConvertDataTableToCSV(dtCSV, writer, true);
        }
    }
    public static void ConvertDataTableToCSV(DataTable objDatatable, TextWriter objTextWriter, bool IsHeader)
    {
        if (IsHeader)
        {
            IEnumerable<String> HeaderText = objDatatable.Columns
                .OfType<DataColumn>()
                .Select(column => ResetBlank(column.ColumnName));

            objTextWriter.WriteLine(String.Join(",", HeaderText));
        }

        IEnumerable<String> items = null;

        foreach (DataRow row in objDatatable.Rows)
        {
            items = row.ItemArray.Select(r => ResetBlank(r == null ? string.Empty :r.ToString()));
            objTextWriter.WriteLine(String.Join(",", items));
        }
        objTextWriter.Flush();
    }
    private static string ResetBlank(string value)
    {
        return String.Concat("\"",
        value.Replace("\"", "\"\""), "\"");
    }
}






No comments:

Post a Comment