foreach (Excel.Worksheet sheet in Book0.Worksheets)
{
Excel.Range usedRange = sheet.UsedRange;
RowID++; Worksheet0.Cells[RowID, 1] = "^^^^" + sheet.Name + "^^^^"; RowID++;
foreach (Excel.Range row in usedRange.Rows)
{
RowID++;
for (int i = 0; i < row.Columns.Count; i++)
/*if (!string.IsNullOrEmpty(rowData[i]))
j = 0;
else
j++;*/
if (!string.IsNullOrEmpty(Convert.ToString(row.Cells[1, i + 1].Value2)))
{
Worksheet0.Cells[RowID, i + 1] = row.Cells[1, i + 1].Value2;
Worksheet0.Cells[RowID, i + 1].Interior.Pattern = row.Cells[1, i + 1].Interior.Pattern;
...
Why? Any other way to efficiently/quickly read the attached two Excel Worksheets?
Excel.Range ws0cell0 = Worksheet0.Cells(1,1);
Excel.Range ws0cell;
foreach (Excel.Worksheet sheet in Book0.Worksheets)
{
Excel.Range usedRange = sheet.UsedRange;
RowID++;
ws0cell0 = "^^^^" + sheet.Name + "^^^^";
RowID++;
foreach (Excel.Range row in usedRange.Rows)
{
RowID++;
ws0cell = ws0Cell0 = ws0Cell0.Offset(1,0); // one row down, first cell
for (int i = 0; i < row.Columns.Count; i++)
/*if (!string.IsNullOrEmpty(rowData[i]))
j = 0;
else
j++;*/
if (!string.IsNullOrEmpty(Convert.ToString(row.Cells[1, i + 1].Value2)))
{
ws0cell.Value2 = row.Cells[1, i + 1].Value2;
ws0cell.Interior.Pattern = row.Cells[1, i + 1].Interior.Pattern;
...
ws0cell = ws0cell.Offset(0,1); // one cell to the rightm sane row.
Doing the same for the source cell should improve performance significantly.
Excel.Worksheet Worksheet0;
Worksheet0 = (Excel.Worksheet)Book0.Worksheets.Add();
Excel.Range ws0cell0 = Worksheet0.Cells[1, 1];
Excel.Range ws0cell;
foreach (Excel.Worksheet sheet in Book0.Worksheets)
{
Excel.Range usedRange = sheet.UsedRange;
RowID++; ws0cell0.Value2 = "^^^^" + sheet.Name + "^^^^"; RowID++;
foreach (Excel.Range row in usedRange.Rows)
{
RowID++;
ws0cell = ws0cell0 = ws0cell0.Offset[1, 0]; // one row down, first cell
for (int i = 0; i < row.Columns.Count; i++)
/*if (!string.IsNullOrEmpty(rowData[i]))
j = 0;
else
j++;*/
if (!string.IsNullOrEmpty(Convert.ToString(row.Cells[1, i + 1].Value2)))
{
ws0cell.Value2 = row.Cells[1, i + 1].Value2;
ws0cell.Interior.Pattern = row.Cells[1, i + 1].Interior.Pattern;
ws0cell.ColumnWidth = row.Cells[1, i + 1].ColumnWidth;
ws0cell.Font.Color = row.Cells[1, i + 1].Font.Color;
ws0cell = ws0cell.Offset[0, 1];
...
but it does still take 20 minutes to process the same Excel file. It is still too long time to process only 2 worksheets, right?
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var ExcelObj = new Excel.Application();
Excel.Workbook Book0 = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
ExcelObj.ScreenUpdating = false;
Excel.Worksheet Worksheet0;
Worksheet0 = (Excel.Worksheet)Book0.Worksheets.Add();
Excel.Range ws0cell0 = Worksheet0.Cells[1, 1];
Excel.Range ws0cell;
foreach (Excel.Worksheet sheet in Book0.Worksheets)
{
Excel.Range usedRange = sheet.UsedRange;
RowID++; ws0cell0.Value2 = "^^^^" + sheet.Name + "^^^^"; RowID++;
foreach (Excel.Range row in usedRange.Rows)
{
RowID++;
row.Copy();
Worksheet0.Cells[RowID, 1].PasteSpecial(-4104);
...
RowID++;
sheet.UsedRange.Copy();
worksheet0.Cells[RowID, 1].PasteSpecial(-4104);
the copy is instantaneous, and cell formatting taken over.