本文介绍了如何使用C#将数据从一个Excel工作表复制到另一个工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好。 我正在尝试比较来自两个不同excel工作表的两个excel单元格值,并将一个值从一个工作表复制到另一个工作表如果两个excel单元格具有相同的值。当我第一次运行代码时,我只在新列中获得1和0。尝试再次运行代码时,列不会填充任何数据。 我做错了什么(...我知道这可能不是最有效的方式...它只是比试图做的更好一个接一个地做。) 代码如下: 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Threading.Tasks; 使用 System.Windows.Forms; 使用 Excel = Microsoft.Office.Interop.Excel; 命名空间 CSExcelTutorial { public partial class Form1:Form { public Form1() { InitializeComponent(); } private void Form1_Load( object sender,EventArgs e) { } private void button1_Click( object sender,EventArgs e) { Excel。应用程序xlApp; // = new Microsoft.Office.Interop.Excel.Application(); Excel。应用程序xlApp1; Excel.Workbook xlWorkBook; Excel.Workbook xlWorkBook1; Excel.Worksheet xlWorkSheet; Excel.Worksheet xlWorkSheet1; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open( c:\\Users\\ctd8z \\ \\\Desktop\\Query for Fred - Student Data.xlsx, 0 , true , 5 , , , true ,Microsoft。 Office.Interop.Excel.XlPlatform.xlWindows, \t, false , false , 0 , true , 1 , 0 ); xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 1 ); xlApp1 = new Excel.Application(); xlWorkBook1 = xlApp1.Workbooks.Open( c:\\Users\\ctd8z \\ \\\Desktop\\Query for Fred - Mentor data.xlsx, 0 , true , 5 , , , true ,Microsoft。 Office.Interop.Excel.XlPlatform.xlWindows, \t, false , false , 0 , true , 1 , 0 ); xlWorkSheet1 =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 1 ); int lastLine = Math.Max( xlWorkSheet.UsedRange.Rows.Count, xlWorkSheet1.UsedRange.Rows 。计数); for ( int row = 2 ; row < = lastLine; row ++) { for ( int row1 = 2 ; row1 < = lastLine; row1 ++) { if (xlWorkSheet.UsedRange.Cells [row, 3 ]。值== xlWorkSheet1.UsedRange.Cells [row1, 3 ]。值) { xlWorkSheet。 UsedRange.Cells [row, 4 ]。Value = Convert.ToString(xlWorkSheet1.UsedRange.Cells [row1, 7 ]值)。 } } } xlWorkBook1.Save(); xlWorkBook.Save(); xlWorkBook1.Close(); xlWorkBook.Close(); xlApp1.Quit(); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp1); releaseObject(xlApp); } private void releaseObject( object obj) { try { System .Runtime.InteropServices.Marshal.ReleaseComObject(OBJ); obj = null ; } catch (例外情况) { obj = null ; MessageBox.Show( 无法释放对象 + ex.ToString()) ; } 最后 { GC.Collect(); } } } } 解决方案 EPPlus dll非常好开放codeplex上的.NET dll可用于对xls和xlsx文件以及宏执行数百次操作和操作。 http:/ /epplus.codeplex.com/releases/view/118053 感谢和问候 Murtuza Patel Hello, all.I'm trying to compare two excel cell values, from two different excel worksheets, and copy a value from one worksheet to the other if the two excel cells have the same value. When I run the code the first time, I only get 1's and 0's in the new column. When attempting to run the code again the column doesn't get filled with any data.What am I doing wrong (...and I know that this may not be the most efficient way of doing it...it is just better than trying to do this one-by-one.)The code is below:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using Excel = Microsoft.Office.Interop.Excel;namespace CSExcelTutorial{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp; //= new Microsoft.Office.Interop.Excel.Application(); Excel.Application xlApp1; Excel.Workbook xlWorkBook; Excel.Workbook xlWorkBook1; Excel.Worksheet xlWorkSheet; Excel.Worksheet xlWorkSheet1; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open("c:\\Users\\ctd8z\\Desktop\\Query for Fred - Student Data.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlApp1 = new Excel.Application(); xlWorkBook1 = xlApp1.Workbooks.Open("c:\\Users\\ctd8z\\Desktop\\Query for Fred - Mentor data.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int lastLine = Math.Max( xlWorkSheet.UsedRange.Rows.Count, xlWorkSheet1.UsedRange.Rows.Count); for (int row = 2; row <= lastLine; row++) { for (int row1 = 2; row1 <= lastLine; row1++) { if (xlWorkSheet.UsedRange.Cells[row, 3].Value == xlWorkSheet1.UsedRange.Cells[row1, 3].Value) { xlWorkSheet.UsedRange.Cells[row, 4].Value = Convert.ToString(xlWorkSheet1.UsedRange.Cells[row1, 7].Value); } } } xlWorkBook1.Save(); xlWorkBook.Save(); xlWorkBook1.Close(); xlWorkBook.Close(); xlApp1.Quit(); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp1); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Unable to release the Object" + ex.ToString()); } finally { GC.Collect(); } } }} 解决方案 EPPlus dll is very good open source .NET dll available on codeplex to perform hundreds of operation and manipulations over xls and xlsx files along with macros.http://epplus.codeplex.com/releases/view/118053Thanks and regardsMurtuza Patel 这篇关于如何使用C#将数据从一个Excel工作表复制到另一个工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 20:56