I found this article:
How can you programmatically import XML data into an Excel file?
which shows how to import an XML file into Excel.
My question is how do I import multiple XML files into multiple sheets in one workbook?
The "OpenXML()" method appears to relate to the Workbooks collection only,
and creates a new Workbook...
Thanks
Use XmlMaps instead of OpenXML and then create new sheets:
Initialize Excel interop
loop...
{
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)Globals.ThisWorkbook.Worksheets.Add();
newWorksheet.Select();
' Run import code
}
We are using EPPlus.dll to generate Excel file. Using EPPlus.dll you can write Excel file in native format.
Following URL will definetly help you to wirte Excel file. But you have to write logic to read XML file and put in Excel workbook.
http://www.jimmycollins.org/blog/?p=547
https://epplus.codeplex.com/
Related
I was developing an application which read data from an excel file, but when I try to open it, an exception was thrown if the source file is saved with the xls format (File contains corrupted data error when opening Excel sheet with OpenXML). indeed when I save this file with the xlsx format it works fine. please help me to solve this problem.
Use Free Spire.XLS dll available via NuGet.
Sample:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xls");
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);
For reliably reading XLS files you could use ExcelDataReader which is a lightweight and fast library written in C# for reading Microsoft Excel files. It supports the import of Excel files all the way back to version 2.0 of Excel (released in 1987!)
Alternatively you could use a file conversion API like Zamzar. This service has been around for 10+ years, and provides a simple REST API for file conversion - it supports XLS to XLSX conversion. You can use it in C# and it has extra features like allowing you to import and export files to and from Amazon S3, FTP servers etc.
Full disclosure: I'm the lead developer for the Zamzar API.
You cannot read xls files with OpenXML.
The solution from Microsoft is to read the xls file with Office Interop (but Interop is not recommended to be used on the server), transfer data from Interop step by step to OpenXML.
Another solution is to use an Excel library like EasyXLS and convert between these two Excel file formats:
ExcelDocument workbook = new ExcelDocument();
workbook.easy_LoadXLSFile("Excel.xls");
workbook.easy_WriteXLSXFile("Excel.xlsx");
Find more information about converting xls to xlsx.
I am not quite sure why you need to convert the file and why you don't just read the xls file, using a different technology then OpenXML, for sure.
XLS is the older Excel file format. XSLX is the newer format stored as OpenXML. XSLX is actually a zip file with the various components stored as files within it. You cannot simply rename the file to get it into the new format. To save the file in XSLX you'll have to save the file into the Excel 2010+ format.
If you're using Excel interop then it is an option on the SaveAs method.
for more info check the function: _Workbook.SaveAs Method
and the property: FileFormat:
Optional Object.
The file format to use when you save the file. For a list of valid choices,
see the FileFormat property. For an existing file, the default format is the
last file format specified; for a new file, the default is the format of the
version of Excel being used.
msdn info here:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas(v=office.11).aspx
I have an excel sheet which is embeded with PDF files. Is there any way to read that embeded PDF files from excel work sheet and saving in to data base using C#.
I believe that u can find some classes in Microsoft.Office.Interop.Excel namespace which can help to extract object embedded into Excel sheet.
for example WorksheetClass class
I was trying to import data set to excel via creating a temporary .csv file in between to speed up the process.
Approach used:
Create a .csv file with the required data.
Open this .csv file using OpenText() function and save it as exel file.
Delete the temp. created .csv file.
Now the problem is when user opts for export to workbook option in that case i had to keep the newly created workbook open to the user as per my application requirement. But since as per my approach file is still opened in the excel workbook. So it's not allowing me the delete the temporary .csv file for obvious reason.
Can anyone suggest me another approach to do this. For exporting i had to use .csv option only, using library closedXML is not in the picture here.
Thanks in advance
I have an automated test which produces a csv with two columns of data.
I have an excel file which I use to gather the results of all the runs in a worksheet.
I want to fully automate the process of updating the Excel file after each test run.
This is probably what I want to do:
1. Read the two columns from the CSV file
2. Paste the two columns in a worksheet in the excel file, in the first
empty column to the right of the existing block of columns.
3. Save the Excel file
EDIT:
Now i understand that i can do steps 1-3 using a macro.
All that is left for me to figure out is how to launch the macro.
You can read the csv and write to the Excel sheet with a single data provider, the OleDb provider. Here is an article on how to write to Excel, and here is one on how to read csv.
Once you write the macro, it should be in the macro list in Excel for that Excel document. You can bind the macro to a keystroke (like Alt-i, Alt-whatever) so that it runs when your press that key combination.
I've done this on a couple of similar projects where I need to import CSV files from other sources and put the data into an Excel sheet (or sheets)
i know this (Create Excel (.XLS and .XLSX) file from C#)and i am using excel libray(http://code.google.com/p/excellibrary/)
By this i can create xls file but i need xlsx file.pls help me.
workbook.Save(file);
there will be an option to specified which format u want in 'Save' method