我正在使用Excel互操作来替换Excel工作簿中的所有逗号值,然后将获取的文件另存为csv。我这样做如下:
var app = new Application();
app.DisplayAlerts = false;
var wb = app.Workbooks.Open(excelfilename);
Worksheet sheet = wb.WorkSheets[0];
sheet.Activate();
Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
Range range = sheet.Range["A1", last];
int lastUsedRow = last.Row;
int lastUsedColumn = last.Column;
var startcell = sheet.Cells[1, 1];
var endcell = sheet.Cells[lastUsedRow, lastUsedColumn];
var subrange = sheet.Range[startcell, endcell];
subrange.Replace(@",", @"", XlLookAt.xlPart, XlSearchOrder.xlByColumns, false, Type.Missing, false, false);
wb.SaveAs(outputfilename, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows);
wb.Close(false, "", true);
但是,我发现上面的代码实际上并没有替换工作表中的所有逗号,在导出到CSV之前,我仍然在工作表中有逗号。我做错了什么?