【问题标题】:Excel spell check using C#使用 C# 进行 Excel 拼写检查
【发布时间】:2012-08-20 15:33:46
【问题描述】:

有人只是帮我解决这个问题! 为什么这段代码不起作用。我在互联网上也找不到太多教程。

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")           

除了每个拼写错误的单词之外,我的输出应该在单元格中包含字符串“chk”。但是输出并没有显示出来。

【问题讨论】:

  • xlApp.CheckSpelling() 的正确用法在这里应该对我有所帮助。有人用过这个吗???!
  • 您可以从在您的 catch 块中添加一个 Debug.WriteLine(ex.Message) 开始。
  • 这是一个令人讨厌的意大利面条代码!不敢相信有人在写这样的 C# 软件……难怪你找不到问题。
  • @walther 如果你能成为足智多谋的人,我会很高兴的。我只需要知道使用 .CheckSpelling() 函数的格式即可。
  • 你需要将你的函数分离为不同的方法。然后从已经建议的方法开始 - 使用调试器并尝试找到问题的根源。你混合和匹配各种抽象层,我可以看到一些数据库访问、一些 xml 库以及谁知道还有什么。如果您根据已知的 OOP 编码标准正确地格式化您的代码,您肯定会发现发现错误突然变得容易得多。

标签: c# excel spell-checking


【解决方案1】:

这段代码终于为我工作了!!!。从 Access Db 中提取数据并作为列存储在 Excel 中,并使用代码识别 Excel 表中拼写错误的单词。

  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)
      {
       }
      }

单元格的选择是让我很忙的部分。终于

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

工作。它对每个拼写错误的单词都显示“chk”。

【讨论】:

    【解决方案2】:

    我认为您可能想查看 MSDN 上的 API,这是链接 =>
    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

    根据定义,CheckSpelling 方法执行此操作,“检查单个单词的拼写。如果在其中一个词典中找到该单词,则返回 True;返回 False > 如果找不到该词。”

    这意味着,如果任何单词拼写错误,CheckSpelling 应该返回 False(取决于该单词是否在给定字典中)

    在您的代码中,您正在执行

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

    我认为这与您想要实现的目标相反。 ("have the string "chk" in the cell besides every wrongly spelled word")

    所以只需添加!到你的 if 语句

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

    这应该是你所追求的。

    另外,为了代码的清晰和可读性,我强烈建议您将代码分解为函数、方法等。在调用xlWorkSheet.Cells[k+2, 1].ToString() 时要小心,因为它可能会在不先检查值的情况下给您 Null 异常.

    【讨论】:

    • 谢谢你!你真的明白我的意思。我确实以相反的方式使用它,当我以相反的方式使用它时,除了我所有的话之外,我还得到了“chk”,为什么您认为会发生这种情况?。如果你能告诉我 ti 是否可能是因为我需要为 .CheckSpelling 工作而键入的参数。理想情况下是 .CheckSpelling(string,default dictionary, dictionary language) 。如您所见,我没有提到这一点,并且我假设该函数将采用默认值。有什么想法吗?我搜索了网络,非常不得不找到一个在 C# 代码中使用过 Application.CheckSpelling 的人。谢谢!
    • 我猜你没有看过链接,是吗?用法实际上都在那里。如果你只想使用默认的拼写检查,你可以这样做.CheckSpelling(xlWorkSheet.Cells[k+2, 1].ToString(), Type.Missing, Type.Missing);
    • 我已经看到了两个链接。甚至在我在论坛上发布问题之前。好吧,看我用过'!现在 。我得到的结果是针对每个字符串的“chk”,即使是正确拼写的单词。现在这是我担心的事情。请@woodykiddy 想法!
    • 我认为你可以做的就是先测试几个单词(最好是硬编码)。不要从数据源中提取数据,也不要使用 for 循环。还要删除使用自定义字典设置的代码。您的自定义词典可能不太正确。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2013-11-20
    相关资源
    最近更新 更多