Hi amritha444,
Instead of adding files directly using AddFile, add binary data using AddEntry to Zip.
Refer below code.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvUsers.DataSource = dt;
gvUsers.DataBind();
}
}
protected void ExportToZip(object sender, EventArgs e)
{
List<string> folderPaths = new List<string>();
for (int i = 0; i < gvUsers.Rows.Count; i++)
{
DataTable dt = new DataTable();
foreach (TableCell cell in gvUsers.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
GridViewRow row = gvUsers.Rows[i] as GridViewRow;
dt.Rows.Add();
for (int j = 0; j < row.Cells.Count; j++)
{
dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
}
string folderPath = Server.MapPath("~/Files/");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string fileName = folderPath + "File" + i + ".xlsx";
SaveInExcel(dt, fileName);
folderPaths.Add(fileName);
}
ZipFile zip = new ZipFile();
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
foreach (string filepath in folderPaths)
{
byte[] bytes = File.ReadAllBytes(filepath);
string fileName = Path.GetFileName(filepath);
zip.AddEntry(fileName, bytes);
}
foreach (string filepath in folderPaths)
{
if (File.Exists(filepath))
{
File.Delete(filepath);
}
}
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();
}
private void SaveInExcel(DataTable dt, string fileName)
{
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dt, "ExcelFile");
wb.SaveAs(fileName);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id", GetType(System.Int32)),
New DataColumn("Name", GetType(System.String)),
New DataColumn("Country", GetType(System.String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvUsers.DataSource = dt
gvUsers.DataBind()
End If
End Sub
Private Sub SaveInExcel(ByVal dt As DataTable, ByVal fileName As String)
Dim wb As XLWorkbook = New XLWorkbook
wb.Worksheets.Add(dt, "ExcelFile")
wb.SaveAs(fileName)
End Sub
Protected Sub ExportToZip(ByVal sender As Object, ByVal e As EventArgs)
Dim folderPaths As List(Of String) = New List(Of String)
Dim i As Integer = 0
Do While (i < gvUsers.Rows.Count)
Dim dt As DataTable = New DataTable
For Each cell As TableCell In gvUsers.HeaderRow.Cells
dt.Columns.Add(cell.Text)
Next
Dim row As GridViewRow = CType(gvUsers.Rows(i), GridViewRow)
dt.Rows.Add()
Dim j As Integer = 0
Do While (j < row.Cells.Count)
dt.Rows((dt.Rows.Count - 1))(j) = row.Cells(j).Text
j = (j + 1)
Loop
Dim folderPath As String = Server.MapPath("~/Files/")
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Dim fileName As String = (folderPath & "File" & i & ".xlsx")
SaveInExcel(dt, fileName)
folderPaths.Add(fileName)
i = (i + 1)
Loop
Dim zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
For Each filepath As String In folderPaths
Dim bytes As Byte() = File.ReadAllBytes(filepath)
Dim fileName As String = Path.GetFileName(filepath)
zip.AddEntry(fileName, bytes)
Next
For Each filepath As String In folderPaths
If File.Exists(filepath) Then
File.Delete(filepath)
End If
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 Sub