Hi Kani,
Check this example. Now please take its reference and correct your code.
For this i example i have refered the below articles. To create zip file you need DotNetZip Library which you can download from the below article.
Namespaces
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Ionic.Zip;
VB.Net
Imports System.Data
Imports Ionic.Zip
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.TableName = "Table1";
dt1.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt1.Rows.Add(1, "John Hammond", "United States");
dt1.Rows.Add(2, "Mudassar Khan", "India");
dt1.Rows.Add(3, "Suzanne Mathews", "France");
dt1.Rows.Add(4, "Robert Schidner", "Russia");
DataTable dt2 = new DataTable();
dt2.TableName = "Table2";
dt2.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt2.Rows.Add(1, "Maria", "Austria");
dt2.Rows.Add(2, "Thomas Hardy", "Ireland");
dt2.Rows.Add(3, "Laurence Lebihan", "USA");
dt2.Rows.Add(4, "Victoria Ashworth", "UK");
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
List<Files> txtDatas = new List<Files>();
foreach (DataTable dt in ds.Tables)
{
byte[] bytes = Encoding.ASCII.GetBytes(ConvertDataTableToString(dt));
txtDatas.Add(new Files() { TableName = dt.TableName, Bytes = bytes });
}
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
foreach (Files txtData in txtDatas)
{
zip.AddEntry(txtData.TableName + ".csv", txtData.Bytes);
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
public static string ConvertDataTableToString(DataTable dt)
{
StringBuilder sb = new StringBuilder();
for (int k = 0; k < dt.Columns.Count; k++)
{
sb.Append(dt.Columns[k].ColumnName + ',');
}
sb.Append("\r\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
sb.Append(dt.Rows[i][k].ToString().Replace(",", ";") + ',');
}
sb.Append("\r\n");
}
return sb.ToString();
}
public class Files
{
public string TableName { get; set; }
public byte[] Bytes { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ds As DataSet = New DataSet()
Dim dt1 As DataTable = New DataTable()
dt1.TableName = "Table1"
dt1.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Country", GetType(String))})
dt1.Rows.Add(1, "John Hammond", "United States")
dt1.Rows.Add(2, "Mudassar Khan", "India")
dt1.Rows.Add(3, "Suzanne Mathews", "France")
dt1.Rows.Add(4, "Robert Schidner", "Russia")
Dim dt2 As DataTable = New DataTable()
dt2.TableName = "Table2"
dt2.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Country", GetType(String))})
dt2.Rows.Add(1, "Maria", "Austria")
dt2.Rows.Add(2, "Thomas Hardy", "Ireland")
dt2.Rows.Add(3, "Laurence Lebihan", "USA")
dt2.Rows.Add(4, "Victoria Ashworth", "UK")
ds.Tables.Add(dt1)
ds.Tables.Add(dt2)
Dim txtDatas As List(Of Files) = New List(Of Files)()
For Each dt As DataTable In ds.Tables
Dim bytes As Byte() = Encoding.ASCII.GetBytes(ConvertDataTableToString(dt))
txtDatas.Add(New Files() With {.TableName = dt.TableName, .Bytes = bytes})
Next
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
For Each txtData As Files In txtDatas
zip.AddEntry(txtData.TableName & ".csv", txtData.Bytes)
Next
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.[End]()
End Using
End Sub
Public Shared Function ConvertDataTableToString(ByVal dt As DataTable) As String
Dim sb As StringBuilder = New StringBuilder()
For k As Integer = 0 To dt.Columns.Count - 1
sb.Append(dt.Columns(k).ColumnName + ","c)
Next
sb.Append(vbCrLf)
For i As Integer = 0 To dt.Rows.Count - 1
For k As Integer = 0 To dt.Columns.Count - 1
sb.Append(dt.Rows(i)(k).ToString().Replace(",", ";") + ","c)
Next
sb.Append(vbCrLf)
Next
Return sb.ToString()
End Function
Public Class Files
Public Property TableName As String
Public Property Bytes As Byte()
End Class
Output
Each DataTable will be saved with .csv extension inside the zip file.