Make an array from all Excel worksheets - c#

I have a C# code that connects to Excel database via OpenFileDialog func. Excel file has too many worksheets and I would like to get all their names in one array and add them to combobox.
For example, this one is the first Excel document.
Any suggestions?

You could try the following:
Accord.IO.ExcelReader reader = new Accord.IO.ExcelReader("yourFile.xlsx");
string[] worksheets = reader.GetWorksheetList();
make sure to include
using Accord.IO;
which you can get Accord from NuGet by searching Accord and Accord.IO.
Then just fill in the items of the Combo box
foreach (string worksheet in worksheets)
{
myComboBox.Items.Add(worksheet);
}

Related

Parsing excel sheet - cell by cell

I would like to see your help on how to parse an excel file in mvc c# but, accessing its cell address like:
var a = Excel_cell{A1:B1}
model.a = a
I've looked to various plugin like excelfilereader, epplus and also javascript libraries like sheetjs and alike but, they do the same where they parse the whole file with the layout of a table...
My excel file is quite complicated because it has no headers and its data is on the right side.
EG:
I apologize if I put an image here...
My requirement is read an excel template, and save its data to the database.. I dont need to send the file to server because it will not be beneficial in the long run
Im hoping you could help me... An idea how to accomplish this would be really help.
You can try by using Spire.XLS, It's a good library that helps you read and write (this one only with a license) excel files and some other formats, it's easy to use and intuitive, for example this would be the code to get an excel file and read some cells:
//Create a variable to access the excel file
Workbook yourFile = new Workbook();
Stream stream = new MemoryStream(FilePath);
//Load the excel file into the variable
workbook.LoadFromStream(stream);
//Select the worksheet you want to take values from
var firstSheet = workbook.Worksheets.Where(x => x.Name == "Name of the sheet").First() as Worksheet;
string value = firstsheet.Range[A1].Text;
So with this code you have populated a variable with the excel file, a variable with a specific sheet (for example the first one), and then you save the text value of a specific cell of your choice in another variable, let me know if I got it right
EDIT: This is their site and this is the nuget gallery

reading xlsx with multi sheets into lists c#

I have a code where i read a file (.csv) and store its columns in lists.
var pathskill = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "skill.csv");
using (var fs1 = File.OpenRead(pathskill))
using (var reader1 = new StreamReader(fs1))
while (!reader1.EndOfStream)
{
var line = reader1.ReadLine();
var values = line.Split(',');
list_MainId.Add(Convert.ToDouble(values[0]));
list_MainName.Add(values[1]);
list_AmountMade.Add(Convert.ToInt32(values[2]));
list_Level.Add(Convert.ToDouble(values[3]));
list_Exp.Add(Convert.ToDouble(values[4]));
list_MadeFrom_One_Id.Add(Convert.ToDouble(values[5]));
list_Amount_MadeFrom_One.Add(Convert.ToInt32(values[6]));
list_MadeFrom_Two_Id.Add(Convert.ToDouble(values[7]));
list_Amount_MadeFrom_Two.Add(Convert.ToInt32(values[8]));
}
This code works great and i get 9 lists with values.
However, i have many .csv files and i think it will be better that each will be like a sheet in xlsx file and i can choose which one to read by its name.
For example to have a sheet called skill1, skill2 and so on.
Is there a way to read a specific sheet from xlsx by its name and store it columns into lists?
Thank you
There are a lot of ways to do this. You could unzip xlsx files and read sheet XMLs directly, or you may use some library that can work with xlsx (e.g. EPPLUS that is freeware).

How to create an excel spreadsheet using a dataset with Npoi

I am completely new to Npoi and I am struggling trying to find some information on how to create a basic spreadsheet from a DataSet.
I have a DataSet being returned that will only ever contain one table I am trying to get the columns and values from these columns into an dynamically generated excel file that I can allow users to download.
So far I have been able to create the workbook and sheet but I can't figure out how to properly populate sheet using the Npoi.dll
My code for creating the workbook (so far) is as follows:
private void CreateWorkbook(DataSet ds)
{
var table = ds.Tables[0];
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet();
foreach (DataColumn col in table.Columns)
{
//seems like I should create the columns for the sheet here
foreach (DataRow row in table.Rows)
{
sheet.CreateRow(); //then populate each column with the approriate data
}
}
}
In addition, and I may be missing something but where is all the documentation Npoi on codeplex mentions, I cannot find anything relevant for Mvc apps dynamically creating a sheet and returning it to the client? What I am trying to accomplish is to create create the file and pass it to the client without storing it on the server.
I'm sure this is basic but I can't figure out where to look for information. I'd appreciate any suggestions.
-cheers
A couple links that got me started:
How to read in XLSX data for editing with NPOI
Creating Excel spreadsheets .XLS and .XLSX in C#
And finally at quick overview of basic functions from the POI documentation like reading a named range, collapsing rows, etc. As NPOI is a close match to POI the guide is fairly effective.
Busy Developers' Guide to HSSF and XSSF Features

How to Get Selected Sheet Names in Excel

I am trying to get names of the selected sheets in Excel. I have 4-5 worksheets in my excel file. User is supposed to select two of them and then my application scans specific columns and compare values. However I could not find a way in C# to get names of the worksheets when user selects more than one sheet. User can also delete these selected worksheets via the application. Any ideas?
Using VSTO:
var sheets = Application.ActiveWindow.SelectedSheets;
var names = new List<string>();
foreach (Excel.Worksheet sh in sheets)
{
names.Add(sh.Name);
}
One way is to use Spreadsheetgear: Link to Spreadsheetgear website
This is the easiest solution to manipulating Excel workbooks I tryed. But quite expensive.
If you're after a free solution, have a look at Excel Package Plus: http://epplus.codeplex.com/
It should have all the features that you require.

How to programmatically group (shift select) sheets in Excel

This topic is related to: What object type are multiple selected sheets in Excel?
To give an overview of the problem, I run some code that changes my selection in Excel, and I want to return the selection to what it originally was (I call this "originalSelection"). In most instances, you can just invoke the Select() method on the originalSelection.
var originalSelection = ExcelApp.Selection;
originalSelection.GetType().InvokeMember("Select", System.Reflection.BindingFlags.InvokeMethod, null, originalSelection , null);
With multiple sheets selected, the selection type is always of a range (it's what Excel defaults to). However, if you have multiple sheets selected, you can run into errors when trying to invoke Select again. You need to do some dancing around to get things to work.
If multiple sheets are selected, but not all the sheets, you can do the follow:
selectedSheets.Select();
activeSheet.Activate();
originalSelection.Select(); //this was casted to an Excel.Range
However, if all the sheets are selected, the activeSheet.Activate() line deselects all the other selected sheets. This also happens if you try it natively using the UI.
I was wondering if there is a way to pragmatically mimic shift-selecting sheets one by one through the code? Closest stuff I've found is stuff with range groupings, but nothing for sheets.
I tried to keep my overview brief, but if you need more clarification on what I'm doing, just ask.
So I figured out a way programatically select sheets.
You can create a string array of names, and use the ordering of the array to get a collection of the sheets. Select this collection, and you should have all the specified sheets selected.
String[] sheetsToBeSelected = {"Sheet3","Sheet1","Sheet2"}; //Note, Sheet3 is the first item in this array
excel.Workbook workbook = ExcelApp.ActiveWorkbook; //get your Excel application however you want
excel.Sheets worksheets = workbook.Worksheets; //get all the sheets in this workbook
//This gets a collection of the sheets specified and ordered by the array of names passed in.
//Just call select on this collection, and the first sheet in the collection becomes the active sheet!
((excel.Sheets)worksheets.get_Item(sheetsToBeSelected)).Select();

Categories

Resources