For Xml
protected void Button5_Click(object sender, EventArgs e)
{
{
int _count = ListSeCh.Items.Count;
if (_count != 0)
{
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Id", typeof(string));
dtTemp.Columns.Add("Name", typeof(string));
for (int j = 0; j <= _count - 1; j++)
{
DataRow dr;
dr = dtTemp.NewRow();
dr["Id"] = ListSeCh.Items[j].Value;
dr["Name"] = ListSeCh.Items[j].Text;
dtTemp.Rows.Add(dr);
}
DataSet ds11 = new DataSet();
ds11.Merge(dtTemp);
if (_count != 0)
{
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Id", typeof(string));
dtTemp.Columns.Add("Name", typeof(string));
for (int j = 0; j <= _count - 1; j++)
{
DataRow dr;
dr = dtTemp.NewRow();
dr["Id"] = ListSeCh.Items[j].Value;
dr["Name"] = ListSeCh.Items[j].Text;
dtTemp.Rows.Add(dr);
}
DataSet ds11 = new DataSet();
ds11.Merge(dtTemp);
ds11.WriteXml(Server.MapPath("ss.xml"));
}
Code for the Zip file of "ss.xml"
FileStream fs = new FileStream(Server.MapPath("ss.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
FileStream compressed = new FileStream("D:\\New Folder\\WebSite\\zipfolder\\ss.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite);
GZipStream gzip = new GZipStream(compressed, CompressionMode.Compress, false);
gzip.Write(data, 0, data.Length);
fs.Close();
gzip.Close();
compressed.Close();
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
FileStream compressed = new FileStream("D:\\New Folder\\WebSite\\zipfolder\\ss.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite);
GZipStream gzip = new GZipStream(compressed, CompressionMode.Compress, false);
gzip.Write(data, 0, data.Length);
fs.Close();
gzip.Close();
compressed.Close();
Code for Csv
//From Dataset
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=CPCB.csv");
Response.Charset = "";
Response.ContentType = "application/text";
StringBuilder sb = new StringBuilder();
for (int k = 0; k < dtTemp.Columns.Count; k++)
{
//add separator
sb.Append(dtTemp.Columns[k].ColumnName + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < dtTemp.Rows.Count; i++)
{
for (int k = 0; k < dtTemp.Columns.Count; k++)
{
//add separator
sb.Append(dtTemp.Rows[i][k].ToString().Replace(",", ";") + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=CPCB.csv");
Response.Charset = "";
Response.ContentType = "application/text";
StringBuilder sb = new StringBuilder();
for (int k = 0; k < dtTemp.Columns.Count; k++)
{
//add separator
sb.Append(dtTemp.Columns[k].ColumnName + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < dtTemp.Rows.Count; i++)
{
for (int k = 0; k < dtTemp.Columns.Count; k++)
{
//add separator
sb.Append(dtTemp.Rows[i][k].ToString().Replace(",", ";") + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
OR
using Excel = Microsoft.Office.Interop.Excel;
public void Excel_FromDataTable(DataTable dt)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
try
{
int iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[1, iCol] = c.ColumnName;
}
int iRow = 0;
foreach (DataRow r in dt.Rows)
{
iRow++;
iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
if (System.IO.File.Exists("D:\\MyExcelWorkBook.csv"))
System.IO.File.Delete("D:\\MyExcelWorkBook.csv");
object missing = System.Reflection.Missing.Value;
workbook.SaveAs("D:\\MyExcelWorkBook",
Excel.XlFileFormat.xlCSV, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlExclusive,
missing, missing, missing, missing, missing);
workbook.Saved = true;
workbook.Close(false, missing, missing);
excel.Quit();
//excel.Visible = true;
}
Response.End();
OR
using Excel = Microsoft.Office.Interop.Excel;
public void Excel_FromDataTable(DataTable dt)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
try
{
int iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[1, iCol] = c.ColumnName;
}
int iRow = 0;
foreach (DataRow r in dt.Rows)
{
iRow++;
iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
if (System.IO.File.Exists("D:\\MyExcelWorkBook.csv"))
System.IO.File.Delete("D:\\MyExcelWorkBook.csv");
object missing = System.Reflection.Missing.Value;
workbook.SaveAs("D:\\MyExcelWorkBook",
Excel.XlFileFormat.xlCSV, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlExclusive,
missing, missing, missing, missing, missing);
workbook.Saved = true;
workbook.Close(false, missing, missing);
excel.Quit();
//excel.Visible = true;
}
No comments:
Post a Comment