How to read Excel in Mac using C# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wonder how we can read an excel file from C# code in a Mac.
Generally we cannot use Microsoft Excel in MAC. I tried to do it by converting the Excel into a .csv and then read the same.
Is there any alternate for this? Any better approach will help
Thanks in advance

I think, you can use the ExcelDataReader library
It's a cross-platform library.
Short sample from github:
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();

Related

NPOI corrupting xlsm workbook

I am required to write some data into an macro enabled workbook which was developed in 1998, i am using NPOI dotnet core library for this task. I am able to write the values into the excel correctly, however i am seeing that alignment and formatting of the sheets is lost. Screenshots below:
First one shows the file after open the file using NPOI and downloading the same, and second shows the file when it is locally opened.
below is the code i am using
public XSSFWorkbook OpenExcelFile(string filePath, string tempSavePath)
{
using (var stream = new FileStream(tempSavePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var temphssfwb = new XSSFWorkbook(OPCPackage.Open(filePath));
temphssfwb.Write(stream);
}
XSSFWorkbook hssfwb;
hssfwb = new XSSFWorkbook(OPCPackage.Open(tempSavePath));
hssfwb.CreateSheet("casevalidationsheet");
return hssfwb;
}
Can someone please help me understand why NPOI is ruining the alignment of my excel workbook?

read data from excel file c# to mongodb database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My application needs to read data from an excel file and stocks it in MongoDB database. I am using .Net and c# for development.I am using Excel 2007 , MongoDB 3.2 and visual studio 2015 version.
Any idea to access excel file, i need your help please.
This is my code
public void Open_readXLS()
{
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Optioncontext ctx = new Optioncontext();
string filePath = #"C:\Users\user PC\Desktop\ finale\Euro_Dollar_Call_Options.xlsx";
workbook = new Excel.Workbook(filePath);
worksheet = workbook.Sheets.GetByName("Feuil1");
for (ushort i = 0; i <= worksheet.Rows.LastRow; i++)
{
option.type_option= worksheet.Rows[i].Cells[0].Value.ToString(),
option.type_currency= worksheet.Rows[i].Cells[1].Value.ToString();
}
ctx.Option.InsertOne(option);
}
There are many ways of achieving this. The simplest would be to save your Excel as a CSV-file for further processing; you can do this in Excel by selecting "Save As" in the "File" menu and then changing the file-ending to CSV. Once you have done this you can use mongoimport to import its contents - no need for C# code in this scenario. You may have to adjust the contents of your CSV so that it fits the structure that is expected by mongoimport; here is a SO post about just that How to use mongoimport to import csv.

C# port of Apache POI corrupts embedded OLE objects in .xlsx

I'm working on a program that processes .xlsx workbooks and reformats them to match a style guide. And on the first sheet of these documents there is an embedded OLE object (a word document) that details aspects of the files.
Whenever I save back my workbook the object gets corrupted, even if skip the first sheet when I execute my processing.
My load and save back code is as follows:
FileStream fStream = new FileStream(file, FileMode.Open, FileAccess.Read);
IWorkbook wb = new XSSFWorkbook(fStream);
fStream.Close();
//do stuff
fStream = new FileStream(file, FileMode.Create, FileAccess.Write);
wb.Write(fStream);
fStream.Close();
The documentation falls short on answering my questions, and previously asked SO questions have remained unanswered so I'm guessing this is either quite a complicated problem or a ridiculously stupid mistake either way I hope you guys can be of assistance.

How to convert stream into Excel sheet (.xls and .xlsx) without any third party tool

Stream inputstream = Exceluploader.PostedFile.InputStream;
if (extenstion == ".xls")
{
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
excelReader = ExcelReaderFactory.CreateBinaryReader(inputstream);
excelReader.IsFirstRowAsColumnNames = true;
}
if (extenstion == ".xlsx")
{
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
excelReader = ExcelReaderFactory.CreateOpenXmlReader(inputstream);
excelReader.IsFirstRowAsColumnNames = true;
}
Currently I am using ExcelDataReader open source to convert the stream into Excel sheet and then into dataset, same I want to perform without any external party tool or dll
Third party tools exist for a reason. It is to make your life easier. If you want to be able to read all of this by yourself without a third party tool, you will need to begin to research the specifications of the XLS and XLSX formats and interpret them yourself.
Checkout these links for XLS and XLSX

How to read all contents of a column of excel file into an array C#? Without using ADO.net approach

How to read all contents of a column from excel file in 1-d array and in C#? Not using ADO.net approach or excel reader. Also there can be any number of rows in the excel file(how to determine number of rows in the column)..some cells may have blank value in the column...need the most basic approach using Microsoft.Office.Interop.Excel
Is using the Excel Objects Library an option?
http://social.msdn.microsoft.com/Forums/en/vsto/thread/b6e8a28c-6760-4e86-a1aa-e2ce9ec36380
If you are using Office 2007 or better, check out OpenXML. Useful for reading and constructing Word, Excel and PowerPoint documents
http://msdn.microsoft.com/en-us/library/bb448854.aspx
A number of 3rd party component vendors have Excel solutions that allow you to load and create Excel files without having to have Excel installed on the system -- I know for a fact Infragistics has such a solution since I have used it myself and it works pretty nicely
Download Excel DataReader This approach is without using ADO.net approach
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();

Categories

Resources