Hi Everybody,
Can you help me to give some solution for below case.
Can you help me to give some solution for below case.
Using C#.Net windows application, I need to browse one excel file and compare the sheet columns.
Ex. Sheet 1: column: A ID
| A ID |
|
|
| 110010 |
| 11002 |
| 110006 |
Sheet 2: column: A1 ID
| A1 ID |
| 110010 |
| 110004 |
| 110006 |
So I need to compare sheet2 values with sheet 1, If the value not
there in sheet2 but present in sheet 1 I need to highlight that cell
using c#.net coding in win. application..
Hope you can understand and please reply soon. any info. needed reply me back.
Thank you very much.
Regards
User

Suthish NairPosted Feb 4, 2011, 2:14 PM
SureshPosted Feb 2, 2011, 3:03 AM
I am posting my code here, can you tell me the problem I am getting HRESULT:.... error under bolded code
private void btnVerify_Click(object sender, EventArgs e)
{
try
{
if (txtFileName.Text.ToString().Trim() != string.Empty && txtFileName.Text.ToString().Trim() != "openFileDailog1")
{
string strCell;
for (int i = 1; i <= intUsedRows; i++)
for (int j = 1; j <= intUsedCols; j++)
{
Excel.Range rg;
rg = (Excel.Range)worksheet.Cells[i, j];
strCell = Convert.ToString(rg.Cells.Value2);
// Common to all sheets
if (strCell.StartsWith(" ") || (strCell.EndsWith(" ")))
{
rg = (Excel.Range)worksheet.Cells[i, j];
rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Tomato);
}
if (strCell.Equals("N/A") || strCell.Equals("NA"))
{
rg = (Excel.Range)worksheet.Cells[i, j];
rg.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);
}
#region Sheet - 2
// Sheet - 2
if (intSelectedSheet == 2)
{
if ((strCell.Trim().Length == 0) || strCell.Contains(" "))
{
rg = (Excel.Range)worksheet.Cells[i, j];
if (j == 1 && i >= 3)
{
rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
Regex objNotNumberPattern = new Regex(@"^\d{1,6}$");
if (j == 7 && i >= 3)
{
if (!objNotNumberPattern.IsMatch(strCell))
{
if (j == 7 && i >= 3)
{
rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
}
}
}
if ((strCell.Trim().Length == 0) || strCell.Contains(" "))
{
}
else
{
Regex objPhoneNumber = new Regex(@"^\d{3}-\d{7}$");
if ((j == 14) && i >= 3)
{
if (!objPhoneNumber.IsMatch(strCell))
{
if ((j == 14) && i >= 3)
{
rg.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
}
if ((strCell.Trim().Length == 0) || strCell.Contains(" "))
{
}
else
{
Regex objPhoneNumber = new Regex(@"^\d{2} \d{4} \d{4}$");
//Regex objPhoneNumber1 = new Regex(@"^\d{4} \d{4} - \d{4}$");
if ((j == 15) && i >= 3)
{
if (!objPhoneNumber.IsMatch(strCell))
{
if ((j == 15) && i >= 3)
{
rg.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
}
}
#endregion
#region Sheet - 3
// Sheet - 3
if (intSelectedSheet == 3)
{
Regex objNotNumberPattern = new Regex(@"^\d{1,6}$");
if ((j == 2 || j == 3) && i >= 3)
{
if (!objNotNumberPattern.IsMatch(strCell))
{
if ((j == 2 || j == 3) && i >= 3)
{
rg.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
DataSet dsApairs5 = GetAPairsExcelData(openFileDialog1.FileName, 3);
DataSet dsAssessors6 = GetAssessorsExcelData(openFileDialog1.FileName, 2);
//CompareAssessorPairsAssessorsids(dsApairs5, dsAssessors6);
StringBuilder sb = new StringBuilder();
foreach (DataRow dr in dsAssessors6.Tables[0].Rows)
{
sb.Append(dr[6] + ",");
}
//foreach (DataRow dr in dsApairs5.Tables[0].Rows)
//{
// if (sb.ToString().IndexOf(strCell) < 0)
// {
// rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
// }
//}
for (int k = 1; k <= dsApairs5.Tables[0].Rows.Count; k++)
{
if (j == 2 && i >= 3)
{
if (sb.ToString().IndexOf(strCell) < 0)
{
if (j == 2 && i >= 3)
{
rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
}
}
#endregion
#region Sheet - 4
// Sheet - 4
if (intSelectedSheet == 4)
{
if ((strCell.Trim().Length == 0) || (strCell.Contains(" ")))
{
rg = (Excel.Range)worksheet.Cells[i, j];
if (j == 4 && i >= 3)
{
rg.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
#endregion
}
CheckIfContinue();
SaveWorkBook();
DisableRadioButtons();
openFileDialog1.Dispose();
txtFileName.Text = string.Empty;
}
else
{
lblSelFile.Visible = true;
}
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
Suthish NairPosted Feb 1, 2011, 2:22 AM
SureshPosted Feb 1, 2011, 1:09 AM
Hi Mr. Suthish Nair,
Thank you for prompt reply. I saw the websites what u suggested its not exactly solves my problem.
I need to compare one excel file two sheets using C# win. application. compare one sheet column values to second sheet column.
Kindly advise me.
Thanks in advance
User
Suthish NairPosted Jan 31, 2011, 1:04 PM
Import Excel Data into GridView & Export to Excel Sheet
How To Open and Read an Excel Spreadsheet into a ListView in .NET
Refer, following links shows how to get data from excel to dataset or sql.