TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Larger Data Writes in Excel File Faster
Abrar Ahmad Ansari
Apr 24
2015
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
Excel = Microsoft.Office.Interop.Excel;
using
System;
using
System.Collections.Generic;
using
System.Diagnostics;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Data;
namespace
LargeDataWriteInExcel
{
class
Program
{
private
static
DataTable GetResult()
{
//get data from db.
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Sl.No"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
dt.Columns.Add(
"Age"
,
typeof
(
int
));
for
(
int
rowIndex = 0; rowIndex < 50000; rowIndex++)
{
var Row = dt.NewRow();
Row[
"Sl.No"
] = rowIndex + 1;
Row[
"Name"
] =
"Abrar "
+ rowIndex + 1;
Row[
"Age"
] = 25;
dt.Rows.Add(Row);
}
return
dt;
}
private
static
Tuple < Object[, ],
int
,
int
> CopyToArray(DataTable _dataTable)
{
int
indexRow = 0;
int
indexCol = 0;
int
totalRows = _dataTable.Rows.Count;
int
countColumns = _dataTable.Columns.Count;
int
startIndex = 0;
object
[, ] objRecored =
new
object
[totalRows + 1, countColumns];
foreach
(DataColumn col
in
_dataTable.Columns)
{
objRecored[indexRow, indexCol++] = col.ToString();
}
indexCol = 0;
indexRow += 1;
for
(startIndex = 0; startIndex < totalRows; startIndex++)
{
foreach
(DataColumn col
in
_dataTable.Columns)
{
objRecored[indexRow, indexCol++] = _dataTable.Rows[startIndex][col].ToString();
}
indexCol = 0;
indexRow++;
}
return
new
Tuple <
object
[, ],
int
,
int
> (objRecored, totalRows + 1, countColumns);
}
private
static
void
GenerateExcel(DataTable _dataTable,
string
_saveFileLocation)
{
try
{
object
[, ] Record =
null
;
int
TotalRowsCount = 1;
int
TotalColumnCount = 1;
if
(_dataTable.Rows.Count > 0)
{
var Items = CopyToArray(_dataTable);
Record = Items.Item1;
TotalRowsCount = Items.Item2;
TotalColumnCount = Items.Item3;
}
Microsoft.Office.Interop.Excel._Application _Application =
new
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook _Workbook = _Application.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet _WorkSheet = (Microsoft.Office.Interop.Excel._Worksheet) _Workbook.Worksheets[1];
_WorkSheet.Name =
"My Data"
;
//-------------------------------------header Range-----------------------
Microsoft.Office.Interop.Excel.Range headerRg_first = (Microsoft.Office.Interop.Excel.Range) _WorkSheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Range headerRg_last = (Microsoft.Office.Interop.Excel.Range) _WorkSheet.Rows.Cells[1, TotalColumnCount];
Microsoft.Office.Interop.Excel.Range headerRange = _WorkSheet.get_Range(headerRg_first, headerRg_last);
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver);
headerRange.EntireRow.Font.Bold =
true
;
headerRange.Font.Name =
"Verdana"
;
headerRange.Font.Size =
"10"
;
headerRange.RowHeight = 22;
headerRange.Borders.Color = System.Drawing.Color.Black;
//------------------------------Record Range--------------------------------------------------------------------------
Microsoft.Office.Interop.Excel.Range RecoredRg_first = (Microsoft.Office.Interop.Excel.Range) _WorkSheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Range RecoredRg_last = (Microsoft.Office.Interop.Excel.Range) _WorkSheet.Cells[TotalRowsCount, TotalColumnCount];
Microsoft.Office.Interop.Excel.Range RecordRange = _WorkSheet.get_Range(RecoredRg_first, RecoredRg_last);
RecordRange.Value = Record;
RecordRange.Borders.Color = System.Drawing.Color.Black;
RecordRange.EntireColumn.AutoFit();
if
(System.IO.File.Exists(_saveFileLocation)) System.IO.File.Delete(_saveFileLocation);
_Workbook.SaveAs(_saveFileLocation, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_Workbook.Close();
_Application.Quit();
}
catch
(Exception ex)
{
//log the error...
}
}
static
void
Main(
string
[] args)
{
DataTable largeDt = GetResult();
GenerateExcel(largeDt,
"c:\\abrar\\a.xls"
);
}
}
}
C#