E
engahmedbarbary
Guest
I working on c# 5 app create more than excel files xlsx
but I face Issue I cannot compress these files to one file as zip archive .
meaning if result of code below of three files as abc.xlsx and autod.xlsx and mog.xlsx as examples
then create all theses file to one file as zip archive with any name but check file creating by zip for these files
not exist before .
my code as below
static void Main(string[] args)
{
DataTable dataTable = GetData();
var res = from row in dataTable.AsEnumerable()
group row by row["Files"] into g
select g;
foreach (var item in res)
{
CreateWorkbook(item.Key.ToString(), item.AsEnumerable().CopyToDataTable());
}
}
public static void CreateWorkbook(String file, DataTable dt)
{
string fileName = file + ".xlsx";
XLWorkbook workbook;
if (!File.Exists(fileName))
{
workbook = new XLWorkbook();
}
else
{
workbook = new XLWorkbook(fileName);
}
var res = from row in dt.AsEnumerable()
group row by row["Tab"] into g
select g;
foreach (var item in res)
{
DataTable dataTable = item.AsEnumerable().CopyToDataTable();
workbook.Worksheets.Add(dataTable, dataTable.Rows[0].Field<string>("Tab"));
}
workbook.SaveAs(fileName);
}
public static DataTable GetData()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("PartId", typeof(int));
dataTable.Columns.Add("Company", typeof(string));
dataTable.Columns.Add("Files", typeof(string));
dataTable.Columns.Add("Tab", typeof(string));
dataTable.Columns.Add("Module", typeof(int));
dataTable.Rows.Add(1222,"micro","Abc","source",1);
dataTable.Rows.Add(1321, "silicon", "Abc", "Types", 3);
dataTable.Rows.Add(1444, "cd2", "AutoD", "Rev", 10);
dataTable.Rows.Add(1321, "cd3", "AutoD", "source", 11);
dataTable.Rows.Add(1541,"mtvscro", "AutoD", "Rev", 12);
dataTable.Rows.Add(9811, "tvs2", "Mog", "Dal", 6);
dataTable.Rows.Add(1901, "tvs3", "Mog", "Mondo", 6);
dataTable.Rows.Add(2111, "toyo", "Mog", "Pingo", 7);
return dataTable;
}
Continue reading...
but I face Issue I cannot compress these files to one file as zip archive .
meaning if result of code below of three files as abc.xlsx and autod.xlsx and mog.xlsx as examples
then create all theses file to one file as zip archive with any name but check file creating by zip for these files
not exist before .
my code as below
static void Main(string[] args)
{
DataTable dataTable = GetData();
var res = from row in dataTable.AsEnumerable()
group row by row["Files"] into g
select g;
foreach (var item in res)
{
CreateWorkbook(item.Key.ToString(), item.AsEnumerable().CopyToDataTable());
}
}
public static void CreateWorkbook(String file, DataTable dt)
{
string fileName = file + ".xlsx";
XLWorkbook workbook;
if (!File.Exists(fileName))
{
workbook = new XLWorkbook();
}
else
{
workbook = new XLWorkbook(fileName);
}
var res = from row in dt.AsEnumerable()
group row by row["Tab"] into g
select g;
foreach (var item in res)
{
DataTable dataTable = item.AsEnumerable().CopyToDataTable();
workbook.Worksheets.Add(dataTable, dataTable.Rows[0].Field<string>("Tab"));
}
workbook.SaveAs(fileName);
}
public static DataTable GetData()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("PartId", typeof(int));
dataTable.Columns.Add("Company", typeof(string));
dataTable.Columns.Add("Files", typeof(string));
dataTable.Columns.Add("Tab", typeof(string));
dataTable.Columns.Add("Module", typeof(int));
dataTable.Rows.Add(1222,"micro","Abc","source",1);
dataTable.Rows.Add(1321, "silicon", "Abc", "Types", 3);
dataTable.Rows.Add(1444, "cd2", "AutoD", "Rev", 10);
dataTable.Rows.Add(1321, "cd3", "AutoD", "source", 11);
dataTable.Rows.Add(1541,"mtvscro", "AutoD", "Rev", 12);
dataTable.Rows.Add(9811, "tvs2", "Mog", "Dal", 6);
dataTable.Rows.Add(1901, "tvs3", "Mog", "Mondo", 6);
dataTable.Rows.Add(2111, "toyo", "Mog", "Pingo", 7);
return dataTable;
}
Continue reading...