public static void Excell(DataSet ds)
{
try
{
string FileName = "F:\\Testing-"+System.DateTime.Now.Hour.ToString()+"-"+DateTime.Now.Minute.ToString()+".xslx";
//DataSet ds = new DataSet("myDataset");
ds.DataSetName = "myDataset";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook ExcelWorkBook = null;
Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = true;
ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>();
for (int j = 0; j < ds.Tables.Count; j++)
{
SheetNames.Add(ds.Tables[j].TableName);
}
for (int i = 1; i < ds.Tables.Count; i++)
ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook
for (int i = 0; i < ds.Tables.Count; i++)
{
int r = 1; // Initialize Excel Row Start Position = 1
ExcelWorkSheet = ExcelWorkBook.Worksheets[i + 1];
//Writing Columns Name in Excel Sheet
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName;
r++;
//Writing Rows into Excel Sheet
for (int row = 0; row < ds.Tables[i].Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn
{
// Excel row and column start positions for writing Row=1 and Col=1
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Rows[row][col - 1].ToString();
r++;
}
ExcelWorkSheet.Name = SheetNames[i];//Renaming the ExcelSheets
}
ExcelWorkBook.SaveAs(FileName);
ExcelWorkBook.Close();
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelWorkSheet);
Marshal.ReleaseComObject(ExcelWorkBook);
Marshal.ReleaseComObject(ExcelApp);
}
catch (Exception ex)
{
}
}
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
{
try
{
string FileName = "F:\\Testing-"+System.DateTime.Now.Hour.ToString()+"-"+DateTime.Now.Minute.ToString()+".xslx";
//DataSet ds = new DataSet("myDataset");
ds.DataSetName = "myDataset";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook ExcelWorkBook = null;
Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = true;
ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>();
for (int j = 0; j < ds.Tables.Count; j++)
{
SheetNames.Add(ds.Tables[j].TableName);
}
for (int i = 1; i < ds.Tables.Count; i++)
ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook
for (int i = 0; i < ds.Tables.Count; i++)
{
int r = 1; // Initialize Excel Row Start Position = 1
ExcelWorkSheet = ExcelWorkBook.Worksheets[i + 1];
//Writing Columns Name in Excel Sheet
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName;
r++;
//Writing Rows into Excel Sheet
for (int row = 0; row < ds.Tables[i].Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn
{
// Excel row and column start positions for writing Row=1 and Col=1
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Rows[row][col - 1].ToString();
r++;
}
ExcelWorkSheet.Name = SheetNames[i];//Renaming the ExcelSheets
}
ExcelWorkBook.SaveAs(FileName);
ExcelWorkBook.Close();
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelWorkSheet);
Marshal.ReleaseComObject(ExcelWorkBook);
Marshal.ReleaseComObject(ExcelApp);
}
catch (Exception ex)
{
}
}
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
No comments:
Post a Comment