如何从单元格中找到一些值并用Excel中的新值替换?
我试过这个,但它不起作用:
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb =default(Microsoft.Office.Interop.Excel.Workbook);
wb = xlapp.Workbooks.Open(FileName.ToString());
wb.Worksheets[0].Cells.Replace("find","replace");
答案 0 :(得分:0)
我建议您使用NPOI,可以通过codeplex或直接通过Visual Studio中的Nuget访问。它使您能够在.NET中轻松上传,编辑和创建电子表格
上传电子表格的示例:
HSSFWorkbook hssfworkbook;
void InitializeWorkbook(string path)
{
//read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
//book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added.
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
hssfworkbook = new HSSFWorkbook(file);
}
}
然后,您可以使用电子表格的IRow和ICell集合在导出之前查找和编辑所需的数据。
可以找到更多示例here
答案 1 :(得分:0)
您所要做的就是替换
wb.Worksheets[0].Cells.Replace("find","replace");
带
wb.Worksheets[1].Cells.Replace("find","replace");
答案 2 :(得分:0)
如果有兴趣,您可以使用GemBox.Spreadsheet,例如:
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
// Load your XLS, XLSX, ODS or CSV file.
ExcelFile wb = ExcelFile.Load(FileName.ToString());
ExcelWorksheet ws = wb.Worksheets[0];
// Replace all "find" occurances with "replace" text.
int row, column;
while(ws.Cells.FindText("find", out row, out column))
ws.Cells[row, column].ReplaceText("find", "replace");
// Save your XLS, XLSX, ODS or CSV file.
wb.Save(FileName.ToString());
您还可以找到另一个searching in Excel example here。