删除非ASCII字符(使用Microsoft.Office.Interop.Excel)

我试图从excel / csv文件中删除所有非ASCII字符。 阅读在线和search后,我发现一个职位,给了我的代码xlWorksheet.UsedRange.Replace("[^\\u0000-\\u007F]"删除字符,但每次,但字符仍然存在于文件中。

另外我得到一个对话框说明

我们找不到任何可替代的东西。 单击“选项”以获取更多search方式。

仅供参考:您试图replace的数据可能位于受保护的工作表中。 Excel无法replace受保护表单中的数据。

不知道如何进一步进行。 我一直在看和在线阅读,但迄今没有发现任何有用的东西。

谢谢您的帮助。

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\username\Desktop\Error Records.csv"); Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; int lastUsedRow = xlWorksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row; int lastUsedColumn = xlWorksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Column; // int lastColumnCount = lastUsedColumn; //; // for (int i = 1; i <= lastUsedColumn; i++) // { // for (int j = 1; j <= lastUsedRow; j++) // { // xlWorksheet.Cells[j, (lastColumnCount+1)] = "Testing data 134"; // } // } xlWorksheet.Cells[1, (lastUsedColumn + 1)] = "Title"; xlWorksheet.UsedRange.Replace("[^\\u0000-\\u007F]", string.Empty); xlWorkbook.Save(); //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); //rule of thumb for releasing com objects: // never use two dots, all COM objects must be referenced and released individually // ex: [somthing].[something].[something] is bad //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet); //close and release xlWorkbook.SaveAs("C:\\Users\\username\\Desktop\\Errors_four.csv".Trim(), Excel.XlFileFormat.xlCSV); xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook); //quit and release xlApp.Quit(); Marshal.ReleaseComObject(xlApp); } } } 

在每个范围的foreach单元格中,您可以使用以下函数来replace当前单元格string值,并清理ascii。 我不知道任何ascii转换函数是本机的Excel互操作库。 我很好奇,你有什么样的例子可以提供你正在尝试转换的东西吗?

还要记住,有function,然后在Excel表中有值。 在你的问题中你不清楚你正在努力合作。 你提到了CSV,这使我认为这些纯粹是VALUES操作。

 public string ReturnCleanASCII(string s) { StringBuilder sb = new StringBuilder(s.Length); foreach(char c in s.ToCharArray()) { if((int)c > 127) // you probably don't want 127 either continue; if((int)c < 32) // I bet you don't want control characters continue; if(c == ',') continue; if(c == '"') continue; sb.Append(c); } return sb.ToString(); } 

这是一个示例用法。 请记住,你将需要弄清楚如何索引单元格自己,这个例子只适用于单元格1,1。 此外,还有两个有用的提示:单元格是1的索引,如果调用Value2而不是Value,则可能会更快。

 // get the value from a cell string dirty = excelSheet.Cells[1, 1].Value.ToString(); // Value2 may be faster! // convert to clean ascii string clean = ReturnCleanASCII(dirty); // set the cell value excelSheet.Cells[1, 1].Value = clean;