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.
Related
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);
}
I need to add selected set of sheets into a pdf using Microsoft.Office.Interop.Excel library in C#. I have been searching throught he net to figure out how to this. But I haven't been able to find anything helpful. Can someone help me with this. Found following statement to convert a given sheet to a pdf. But it looks like, it cannot append list of excel sheets into one pdf when we provide sheet names.
xlws.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, endPath);
Help please.
You can use the Worksheet.ExportAsFixedFormat method.
Example:
string exportFilePath = #"C:\Desktop\Test.pdf";
activeWorksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, exportFilePath);
Or you can also use the Workbook.ExportAsFixedFormat method
string exportFilePath = #"C:\Desktop\Test.pdf";
activeWorksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, exportFilePath);
References:
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.exportasfixedformat.aspx
https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.exportasfixedformat.aspx
I'm fiddling with Google Sheets AP v4, starting with this introduction. The API seems pretty clear but I can't figure out how to enumerate existing spreadsheets. Is that not a supported feature for some reason or am I missing something?
You should be able to get the list of sheet names by using this code:
var spreadsheets = service.Spreadsheets.Get(spreadSheetId).Execute();
var titles = spreadsheets.Sheets.Select(s => s.Properties.Title).ToArray();
Sheets get returns a sheet resource which contains a list of sheets within a sheet.
So it's just a matter of getting the sheet you are interested in and you will have a list of the sheets it contains
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
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();