protected void ExportExcel(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0) return;
var xlApp = new Excel.Application();
//Is this used?
CultureInfo CurrentCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Range range;
Excel.Workbook workbook = workbooks.Add();
Excel.Worksheet worksheet = workbook.Worksheets[1];
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0;
//xlApp.worksheet.Properties.Title = @"This co";
// worksheet.Cells[1, 3] = "BANAVATHY POWER SYSTEMS PVT. LTD.";
for (var i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = worksheet.Cells[1, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
range.Borders.Color = Color.Green;
}
for (var r = 0; r < dt.Rows.Count; r++)
{
for (var i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString();
}
rowRead++;
//is this used?
percent = ((float)(100 * rowRead)) / totalCount;
}
Microsoft.Office.Interop.Excel.Range columns = worksheet.UsedRange.Columns;
columns.AutoFit();
worksheet.Activate();
worksheet.Application.ActiveWindow.SplitRow = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
// Now apply autofilter
Excel.Range firstRow = (Excel.Range)worksheet.Rows[1];
firstRow.Activate();
firstRow.Select();
//firstRow.AutoFilter(1,
// Type.Missing,
// Excel.XlAutoFilterOperator.xlAnd,
// Type.Missing,
// true);
//worksheet.Activate();
//worksheet.Application.ActiveWindow.SplitColumn = 1;
//worksheet.Application.ActiveWindow.FreezePanes = true;
//worksheet.Rows[1].Insert();
//Excel.Range newRow = worksheet.Rows[1];
//Excel.Range newCell = newRow.Cells[9];
//newCell.MergeCells = true;
//newCell.Value2 = "BANAVATHY POWER SYSTEMS PVT. LTD:Sales Invoice";
//newCell.Value2="Item Wise Sales Report";
// newCell.Font.Bold = true;
// newCell.Font.Color = Color.Green;
//newCell.Value = DateTime.Now.ToString("yyyy-MM-dd");
dt.Clear();
xlApp.Visible = true;
}
{
if (dt == null || dt.Rows.Count == 0) return;
var xlApp = new Excel.Application();
//Is this used?
CultureInfo CurrentCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Range range;
Excel.Workbook workbook = workbooks.Add();
Excel.Worksheet worksheet = workbook.Worksheets[1];
long totalCount = dt.Rows.Count;
long rowRead = 0;
float percent = 0;
//xlApp.worksheet.Properties.Title = @"This co";
// worksheet.Cells[1, 3] = "BANAVATHY POWER SYSTEMS PVT. LTD.";
for (var i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = worksheet.Cells[1, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
range.Borders.Color = Color.Green;
}
for (var r = 0; r < dt.Rows.Count; r++)
{
for (var i = 0; i < dt.Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString();
}
rowRead++;
//is this used?
percent = ((float)(100 * rowRead)) / totalCount;
}
Microsoft.Office.Interop.Excel.Range columns = worksheet.UsedRange.Columns;
columns.AutoFit();
worksheet.Activate();
worksheet.Application.ActiveWindow.SplitRow = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
// Now apply autofilter
Excel.Range firstRow = (Excel.Range)worksheet.Rows[1];
firstRow.Activate();
firstRow.Select();
//firstRow.AutoFilter(1,
// Type.Missing,
// Excel.XlAutoFilterOperator.xlAnd,
// Type.Missing,
// true);
//worksheet.Activate();
//worksheet.Application.ActiveWindow.SplitColumn = 1;
//worksheet.Application.ActiveWindow.FreezePanes = true;
//worksheet.Rows[1].Insert();
//Excel.Range newRow = worksheet.Rows[1];
//Excel.Range newCell = newRow.Cells[9];
//newCell.MergeCells = true;
//newCell.Value2 = "BANAVATHY POWER SYSTEMS PVT. LTD:Sales Invoice";
//newCell.Value2="Item Wise Sales Report";
// newCell.Font.Bold = true;
// newCell.Font.Color = Color.Green;
//newCell.Value = DateTime.Now.ToString("yyyy-MM-dd");
dt.Clear();
xlApp.Visible = true;
}
No comments:
Post a Comment