简体   繁体   English

如何使用 C# 将选定列的所有行从一个 excel 文件复制到另一个文件

[英]How to copy all the rows of selected column from one excel file to another using C#

I am using this code, but since I want to copy a whole row with particular attribute and I don't know the count of totals rows.我正在使用此代码,但由于我想复制具有特定属性的整行并且我不知道总计行的计数。

        Excel.Application excelApplication = new Excel.Application();

        srcPath = "C:\\Users\\sn314708\\Desktop\\Excel\\1.xlsx";
        Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
        Excel.Worksheet srcworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)srcworkBook.Sheets.get_Item(1);

        destPath = "C:\\Users\\sn314708\\Desktop\\Excel\\2.xlsx";
        Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath, 0, false);
        Excel.Worksheet destworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)destworkBook.Sheets.get_Item(1);

        Excel.Range from = srcworkSheet.Range["A1:C100"];
        Excel.Range to = destworkSheet.Range["A1:C100"];


        from.Copy(to);

        destworkBook.SaveAs("C:\\Users\\sn314708\\Desktop\\Excel\\2 " + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");

either you fix the max number of rows you want to copy, or you could find the last row used in the whole sheet (i think its better).要么您修复要复制的最大行数,要么您可以找到整个工作表中使用的最后一行(我认为它更好)。 A sample you could do:你可以做的一个样本:

int lastUsedRow = srcworkSheet.Cells.Find("*", System.Reflection.Missing.Value,
                     System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                     XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious,
                     false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;


int colIndex = 1;
for(int rowIndex = 1; rowIndex <= lastUsedRow; rowIndex++ )
{
    destworkSheet.Cells[rowIndex, colIndex] = srcworkSheet.Cells[rowIndex, colIndex];
}

index of row and column always start at 1.行和列的索引总是从 1 开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#在Autocad中将所选实体从一个工程图复制到另一个工程图 - How to Copy Selected Entities From One Drawing to Another in Autocad Using C# 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# 如何将特定单元格(带有值、颜色、字体)从一个 excel 文件复制到另一个 excel 文件 C#? - How can I copy specific cells(with Value,Color,Font) from one excel file to another excel file C#? 使用C#中的cmd将文件从一个目录复制到另一个目录 - Copy file from one directory to another using cmd in C# 将所有选定的(选中的)TreeNode从一个树形视图复制到另一个(包括未选中的父级)C# - Copy all Selected (Checked) TreeNodes from one treeview to another (include unchecked parents) C# 如何在C#中将文件从一个文件夹复制到另一个文件夹? - How to copy file from one folder to another in C#? 如何在C#中将文件从一个位置复制到另一个位置 - How to copy a file from one location to another in C# 将特定的列从一个Excel电子表格复制到C#中的另一列 - Copy specific columns from one excel spreadsheet to another in C# C# 代码将所有表从一个 mdb 文件复制到另一个 mdb 文件 - C# code to copy all the tables from one mdb file to another mdb file 如何将Excel文件的选定列和行导入到ListView C# - How to import selected columns and rows of excel file to listView c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM