Excel spell check using C#

落爺英雄遲暮 提交于 2019-12-02 07:55:48

问题


some one just help me with this! why isn't this code working.I don't find much tutorials on the internet too.

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkShee=(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);                 
xlApp.SpellingOptions.UserDict = "CUSTOM.DIC";     
var udict = xlApp.SpellingOptions.UserDict;
xlWorkSheet.CheckSpelling();        
xlWorkSheet.Cells[1, 1] = "Sstring";           
string tsql = "select nvalue from [role report]";
OleDbDataAdapter tda = new OleDbDataAdapter(tsql, con);
DataTable tdt = new DataTable();
con.Open();
tda.Fill(tdt);
con.Close();
int count = 0;

for (int x = 0; x<500; x++)
{
    if (tdt.Rows[x]["nvalue"].ToString()!= "")
    {
        xlWorkSheet.Cells[x+2, 1] = tdt.Rows[x]["nvalue"].ToString();
        count++;
    }
}

for (int k=0; k<count; y++)
{
     //bool t = false;
    if (xlWorkSheet.Cells[k+2, 1].ToString() != "")
    {
        if ((xlApp.CheckSpelling(xlWorkSheet.Cells[k+2, 1].ToString())))
            xlWorkSheet.Cells[k+2, 2] = "chk";
    }
}

try
{
    xlWorkBook.SaveAs("spellspell.xls",Excel.XlFileFormat.xlWorkbookNormal,
    misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,                      
    misValue, misValue,misValue,misValue);
}
catch (Exception ex)
{ }
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created, you can find the file c:\\csharp-Excel.xls")           

My output is supposed to have the string "chk" in the cell besides every wrongly spelled word. But the output doesn't show that.


回答1:


This code worked finally for me!!!. Pulled the data from Access Db and stored as a column in Excel and used the code to identify the wrongly spelled words in the excel sheet.

  for (int y = 0; y <count; y++)
    {
    try
       {
   if(xlWorkSheet.Range["A" + (y + 2) + ""].Value.ToString() != "")
      {
     if (!(xlApp.CheckSpelling(xlWorkSheet.Range["A" + (y + 2) + ""].Value.ToString(), 
             udict, 1033)))
           {
              xlApp.Visible = false;

              xlWorkSheet.Cells[y + 2, 2] = "chk";            
                 }
              }
          }
     catch(Exception)
      {
       }
      }

The selection of the cell was the part which got me busy. Finally

 if (!(xlApp.CheckSpelling(xlWorkSheet.Range["A" + (y + 2) + ""].Value.ToString(), 
         udict, 1033)))

worked. It showed "chk" against every wrongly spelled word.




回答2:


I think you might want to check out the APIs on MSDN, here's the links =>
Worksheet.CheckSpelling:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.checkspelling(v=vs.100).aspx

Application.CheckSpelling:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.checkspelling

According to the definition, CheckSpelling method does this, "Checks the spelling of a single word. Returns True if the word is found in one of the dictionaries; returns False if the word isn't found."

That means, if any word is misspelled, CheckSpelling should return False (depends on whether the word is in the given dictionary or not)

In your code, you were doing

if ((xlApp.CheckSpelling(xlWorkSheet.Cells[k+2, 1].ToString())))
    xlWorkSheet.Cells[k+2, 2] = "chk";

which I think is the opposite to what you were trying to achieve. ("have the string "chk" in the cell besides every wrongly spelled word")

So just add ! to your if statement

if (!(xlApp.CheckSpelling(xlWorkSheet.Cells[k+2, 1].ToString())))
    xlWorkSheet.Cells[k+2, 2] = "chk";

and that should be what you were after.

Also, for code clarity and readability, I'd highly suggest that you break down your code into functions, methods, etc. And be careful about calling xlWorkSheet.Cells[k+2, 1].ToString(), as it might give you Null exception without checking the value first.



来源:https://stackoverflow.com/questions/12120082/excel-spell-check-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!