首页 > 代码库 > 使用C# 将Excel1 中sheet1 的数据复制粘贴到Excel2中的sheet2

使用C# 将Excel1 中sheet1 的数据复制粘贴到Excel2中的sheet2

最近在项目中有这个需求, 在网上找的资料哦也有限 。

最开始想的是用openxmlsdk  只能在同一个Excel表中数据复制!

 

1、使用openxml sdk 复制Excel 中Sheet1 的数据到sheet2;

 1  /// <summary>
 2         ///使用openxmlsdk 转换数据 原理是通过CloneNode克隆  目前只完成同表之间的全部复制! 
 3         /// </summary>
 4         /// <param name="path">Excel路径</param>
 5         /// <param name="sheet1"></param>
 6         /// <param name="sheet2"></param>
 7         public static void Dome2(string path1, string sheet1, string sheet2)
 8         {
 9             SpreadsheetDocument document1 = SpreadsheetDocument.Open(path1, false);
10 
11             Sheet sheet2 = document1 .WorkbookPart.Workbook.Descendants<Sheet>().Where(p => p.Name == sheet2).First();
12             WorksheetPart worksheet2 = (WorksheetPart)document1 .WorkbookPart.GetPartById(sheet2.Id);
13 
14             ////查找目标模版Sheet 页
15             WorksheetPart sourceWorksheetPart = SpreadsheetReader.GetWorksheetPartByName(document1, sheet1);
16 
17             //通过深拷贝的方式直接拷贝目标模版的数据格式部分的 XML。
18             worksheet2.Worksheet = (Worksheet)sourceWorksheetPart.Worksheet.CloneNode(true);
19 
20             worksheet2.Worksheet.Save();
21             sourceWorksheetPart.Worksheet.Save();
22 
23         }

2、使用vs 自带的Excel插件完成 两表之间的数据转换

 1  /// <summary>
 2         /// 
 3         /// </summary>
 4         /// <param name="filepath"></param>
 5         /// <param name="filepath2"></param>
 6         public static void Dome(string filepath, string filepath2)
 7         {
 8             //
 9             Excel1.Application excel = new Excel1.Application();
10             excel.Visible = false;
11             excel.DisplayAlerts = false;
12             //打开Excel1
13             Excel1.Workbook workbook = excel.Workbooks._Open(filepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value
14                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
15                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
16             //打开Excel2
17             Excel1.Workbook workbook2 = excel.Workbooks._Open(filepath2, Missing.Value, Missing.Value, Missing.Value, Missing.Value
18               , Missing.Value, Missing.Value, Missing.Value, Missing.Value
19               , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
20 
21             //
22             Excel1.Worksheet worksheet1 = (Excel1.Worksheet)workbook.Worksheets[1];
23             Excel1.Worksheet worksheet2 = (Excel1.Worksheet)workbook2.Worksheets[1];
24          //
25             Excel.Range range = worksheet1 .Cells;
26             range1.Copy(Missing.Value);
27             worksheet2 .Paste(Missing.Value, Missing.Value);
28 
29             workbook1.Save();
30             workbook2.Save();
31         }            

 

3、 完成Excel1中Sheet1 指定位置数据 复制到Excel2中Sheet2 指定位置;

 1 public static void Dome(string filepath, string filepath2, string rangeReference)
 2         {
 3             //
 4             Range range = new Range(rangeReference);
 5             Excel1.Application excel = new Excel1.Application();
 6             excel.Visible = false;
 7             excel.DisplayAlerts = false;
 8             //
 9             Excel1.Workbook workbook1 = excel.Workbooks._Open(filepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value
10                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
11                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
12 
13             Excel1.Workbook workbook2 = excel.Workbooks._Open(filepath2, Missing.Value, Missing.Value, Missing.Value, Missing.Value
14               , Missing.Value, Missing.Value, Missing.Value, Missing.Value
15               , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
16 
17             //
18             Excel1.Worksheet worksheet1 = (Excel1.Worksheet)workbook.Worksheets[1];
19             Excel1.Worksheet worksheet2 = (Excel1.Worksheet)workbook2.Worksheets[1];
20             
21             //sheet1位置
22             Excel1.Range range1 = worksheet.Range[worksheet.Cells[range.StartRow, range.StartCol], worksheet.Cells[range.EndRow, range.EndCol]];
23             //sheet2位置
24             Excel1.Range range2 = worksheet1.Range[worksheet1.Cells[range.StartRow, range.StartCol], worksheet1.Cells[range.EndRow, range.EndCol]];
25            
26             range1.Copy(Missing.Value);
27             worksheet2.Paste(range2, false);
28 
29             workbook2.Save();
30             workbook1.Save();
31         }            

 

使用C# 将Excel1 中sheet1 的数据复制粘贴到Excel2中的sheet2