2012-08-25 57 views
1

有人只是帮助我这个! 为什么不是这个代码工作。我也没有在互联网上找到太多的教程。Excel的拼写检查使用C#

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”,除了每​​个错误拼写的单词。但是输出并没有显示出来。

+0

xlApp.CheckSpelling()的正确用法应该可以帮助我。任何人之前使用过这个???! – lunchbox

+0

您可以从您的catch块添加Debug.WriteLine(ex.Message)开始。 –

+0

这是一个令人讨厌的意大利面代码!不能相信某人正在编写这样的C#软件......难怪你找不到问题。 – walther

回答

0

我想你可能想看看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如果该词在其中一个词典中找到;如果这个词是没有找到返回。”

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

在你的代码,你在做

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 (!(xlApp.CheckSpelling(xlWorkSheet.Cells[k+2, 1].ToString()))) 
    xlWorkSheet.Cells[k+2, 2] = "chk"; 

而这应该是你以后。另外,为了代码的清晰性和可读性,我强烈建议您将代码分解为函数,方法等。并且要小心地调用xlWorkSheet.Cells[k+2, 1].ToString(),因为它可能会在不检查值的情况下为您提供Null异常。

+0

谢谢你!你其实明白我的意思。我反过来用了它,当我用相反的方式时,除了我所有的话之外,我还得到了“chk”,为什么你会这么想呢?如果你能告诉我是否可能是因为我需要为.CheckSpelling工作的关键参数。理想情况是.CheckSpelling(字符串,默认字典,字典语言)。正如你所看到的,我没有提到这一点,并且我推测这个函数会采用默认值。任何想法?我搜索网络,非常需要找到一个在C#代码中使用Application.CheckSpelling的人。谢谢! – lunchbox

+0

我猜你没有看过链接,是吗?用法实际上都在那里。如果你只是想使用默认的拼写检查,你可以这样做'.CheckSpelling(xlWorkSheet.Cells [k + 2,1] .ToString(),Type.Missing,Type.Missing);' – woodykiddy

+0

我看到这两个链接已经是人。甚至在我发布论坛上的问题之前。好吧,看我用'!'现在。我得到的结果是对每一个字符串“chk”,即使拼写正确的单词也是如此。现在我很担心这件事。 @woodykiddy的想法请! – lunchbox

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”针对每个错误拼写的单词。