1

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

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”。但是输出并没有显示出来。

4

2 回答 2

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”。

于 2012-08-27T11:54:53.217 回答
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;如果未找到该单词,则返回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 异常而不先检查值。

于 2012-08-26T04:26:21.710 回答