C# 删除非ASCII字符(使用Microsoft.Office.Interop.Excel)
我正在尝试从excel/csv文件中删除所有非ascii字符。在线阅读和搜索后,我发现了一篇文章,其中给出了代码C# 删除非ASCII字符(使用Microsoft.Office.Interop.Excel),c#,.net,excel,C#,.net,Excel,我正在尝试从excel/csv文件中删除所有非ascii字符。在线阅读和搜索后,我发现了一篇文章,其中给出了代码xlsheetwork.UsedRange.Replace(“[^\\u0000-\\u007F]”,以删除字符,但每次字符仍存在于文件中 我还得到一个对话框,说明 我们找不到任何可替换的内容。单击“选项”可获取更多方法 搜索 仅供参考:您尝试替换的数据可能处于受保护的状态 工作表。Excel无法替换受保护工作表中的数据 我不知道如何继续。我一直在网上寻找和阅读,但到目前为止没有发现任
xlsheetwork.UsedRange.Replace(“[^\\u0000-\\u007F]”
,以删除字符,但每次字符仍存在于文件中
我还得到一个对话框,说明
我们找不到任何可替换的内容。单击“选项”可获取更多方法
搜索
仅供参考:您尝试替换的数据可能处于受保护的状态
工作表。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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.InteropServices;
使用系统文本;
使用System.Threading.Tasks;
使用Excel=Microsoft.Office.Interop.Excel;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Excel.Application xlApp=新的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=xlsheet.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)。行;
int lastUsedColumn=xlsheet.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)列;
//int lastColumnCount=lastUsedColumn;
//;
//为了(int i=1;i对于每个区域中的每个单元格,您可以使用以下函数将当前单元格字符串值替换为清理后的ascii。我不知道excel互操作库中有任何本机ascii转换函数。我很好奇,您是否有任何示例可以提供您尝试转换的内容
请记住,excel表格中有函数,也有值。在您的问题中,您不清楚要使用哪一个。您提到了CSV,这使我认为这些纯粹是值操作
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();
}
我不知道如何将xlWorkSheet传递给字符串生成器。您能解释一下吗?谢谢您的回答。下面是关于我正在实现什么的问题。谢谢(int i=1;i这看起来不错,但我没有通过调试器运行它或编译它。您的想法是正确的。Excel不支持正则表达式样式的替换,因此您需要循环遍历每个单元格,将内容提取为字符串,并对字符串进行替换,然后将其分配回单元格。
// 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;