我使用interlop库从Excel文件复制粘贴值。我想使用openXML来完成此操作,因为我必须将其安装在未安装Excel的服务器上。
public static void Excel(string filepathdl)
{
Excel.Application srcxlApp;
Excel.Workbook srcworkBook;
Excel.Worksheet srcworkSheet;
Excel.Worksheet destworkSheet;
Excel.Range srcrange;
Excel.Range destrange;
//Opening of first worksheet and copying
srcxlApp = new Excel.Application();
srcworkBook = srcxlApp.Workbooks.Open(filepathdl);
srcworkSheet = srcworkBook.Worksheets.get_Item(2);
srcrange = srcworkSheet.UsedRange;
srcrange.Copy();
destworkSheet = srcworkBook.Worksheets.Add();
destworkSheet.Name = "Comprehend Data Clean";
destrange = destworkSheet.Cells[1, 1];
destrange.Select();
destrange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValuesAndNumberFormats, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
srcworkBook.Save();
srcxlApp.Application.DisplayAlerts = false;
srcworkBook.Close(false, null, null);
srcxlApp.Quit();
}