2017-06-29 137 views
2

我試圖從excel/csv文件中刪除所有非ascii字符。在閱讀在線和搜索後,我發現一篇文章給我的代碼xlWorksheet.UsedRange.Replace("[^\\u0000-\\u007F]"刪除字符,但每次,但字符仍然存在於文件中。刪除非ASCII字符(使用Microsoft.Office.Interop.Excel)

而且我得到一個對話框,說明

我們無法找到任何東西來代替。點擊選項查看 搜索的更多方法。

僅供參考:您試圖替換的數據可能位於受保護的 工作表中。 Excel無法替換受保護工作表中的數據。

不確定如何進一步處理。我一直在網上閱讀和閱讀,但迄今沒有發現任何有用的東西。

感謝您的幫助。

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

     } 
    } 
} 
+0

Excel不支持Regex樣式替換,因此您需要遍歷每個單元格,將內容作爲字符串提取並對字符串進行替換並將其分配回單元格。 – PaulF

回答

0

foreach cell在每個範圍內,可以使用下面的函數來替換當前單元格的字符串值,並用ascii清理。我不知道任何excel互操作庫本地的ascii轉換函數。我很好奇,你有什麼樣的例子可以提供你正試圖轉換的東西嗎?

請記住,有功能,然後在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; 
+0

我不知道如何將xlWorkSheet傳遞給字符串生成器。你能解釋一下嗎? – Maddy

+0

當然,請檢查我的編輯。 – sapbucket

+0

謝謝你的回覆。只是關於我正在實施的一個以下問題。 對(INT I = 1;我<= lastUsedRow;我++) { 對(INT J = 1;Ĵ<= lastUsedColumn; J ++){ 串 髒= xlWorksheet.Cells [I,J]。價值的ToString(); string clean = ReturnCleanASCII(dirty); xlWorksheet.Cells [i,j] .Value = clean; } } 這看起來不錯嗎? – Maddy