I would like to copy Excel columns using Powershell.

Asked 2 years ago, Updated 2 years ago, 109 views

I would like to copy the specified column from Excel using powershell.

For example,
How do I select only columns A, D, and F in Excel 1 and paste them into columns A, B, and C in Excel 2?

Thank you for your cooperation.

powershell

2022-09-30 19:44

2 Answers

PowerShell provides references to COM and .NET objects, so if Excel is deployed on the running machine,

$app=New-Object-ComObjectExcel.Application

// Actual Excel Processing
$app.DisplayAlerts=$false
...

It is recommended that you operate the COM interface in Excel as shown in .

If you cannot select the above, include the NPOI assembly and

// Load all reference libraries
System.Reflection.Assembly :: LoadFrom("NPOI.OpenXml4Net.dll");
System.Reflection.Assembly :: LoadFrom("NPOI.OpenXmlFormats.dll");
System.Reflection.Assembly :: LoadFrom("ISharpCode.SharpZipLib.dll";
System.Reflection.Assembly::LoadFrom("NPOI.dll");
System.Reflection.Assembly::LoadFrom("NPOI.OOXML.dll");

$wb = New-Object NPOI.XSSF.UserModel.XSSFWorkbook("excel1.xlsx");
...

It can also be used with .

There is little to do with PowerShell about how each library achieves its requirements, so look for samples from VBA, C#, and more.


2022-09-30 19:44

Excel information seems to be frequently asked, so I'll share the latest situation.
Just as Excel operation from COM is difficult in any language, PowerShell is difficult to touch directly.

Currently, the PowerShell community often uses a module called ImportExcel.
https://github.com/dfinke/ImportExcel

I would like you to check the repository for more information on how to use it, but PowerShell 5.0 and later can also be installed with just one command, so you should try it.

Install-Module ImportExcel-scope CurrentUser

Reddit and other usage samples are available.

https://www.reddit.com/r/PowerShell/comments/51h101/basic_questions_about_using_importexcel/


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.