简体   繁体   中英

Using vb.net code for unzipping an Excel file and manipulating its XML structure

I have a situation where in I need to perform the below steps using VB.net code:

1) Rename an Excel file to .zip (not actually zipping the file, but just renaming it to . zip)

2) Unzip the renamed Excel file to access it's XML structure. The XML structure of the excel sheet is stored in the file name sheet.xml, so the goal is to access this XML file.

3) Manipulate the sheet.xml file by rearranging the column structure.

As of now, I am trying to focus on steps 1 and 2 alone. Step 1 can be achieved easily, since it involves only renaming the excel file to .zip, without performing any compression.

But I am not sure about step 2, since it is required to unzip the excel file for its XML structure, without actually zipping it before.

Can anyone shed some light on how step 2 can be achieved?

An XLSX-File is just a zip-container with a collection of XML files inside.
Thus you can extract the files from it by renaming it to .zip .

See also: How to properly assemble a valid xlsx file from its internal sub-components?

if you are using .NET Framework 4 or higher you can use the included ZipFile helper class.

Imports System.IO
Imports System.IO.Compression;

Dim filename as String = "c:\Temp\Input.xlsx"
Dim extractDirectory as String = "c:\Temp\ExtractedExcel"

Directory.CreateDirectory(extractDirectory)
ZipFile.ExtractToDirectory(filename, extractDirectory);

You need to make sure to reference System.IO.Compression and System.IO.Compression.FileSystem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM