I use the Open from the Assets on xamarin then and i Open on the Workbooks when i build my app it stoped working but if i coding on normal Console it's working, What i forget ?
var asset = Application.Context.Assets.Open("test.xlsx");
new ExcelEngine().Excel.Workbooks.Open(asset);
https://help.syncfusion.com/file-formats/xlsio/create-read-edit-excel-files-in-xamarin-c-sharp
In Android, you could try the code below.
string filePath="test.xlsx";
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
var asset = Android.App.Application.Context.Assets.Open(filePath);
using (StreamReader sr = new StreamReader(asset))
{
var inputStream = sr.BaseStream;
//Open the Tab delimited CSV file
IWorkbook workbook = application.Workbooks.Open(inputStream, "\t");
IWorksheet sheet = workbook.Worksheets[0];
var s = sheet.Columns.Count();
}
}
The code works well on my side to get the Column count of the Excel.
If you want to use this in Xamarin.Forms, you could use the Dependency Service. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
Related
I have a file in blob storage that is in .xslx format and I am trying to convert it to .csv format using Syncfusion.XlsIO Nuget package.
I have tried the following:
private async Task ConvertExcelToCsv()
{
var fileName = await _fileStore.GetContainerFileAsync(AppSettingsConstants.FileNames.Container, "myfile.xlsx");
using (ExcelEngine excelEngine = new ExcelEngine())
{
var application = excelEngine.Excel;
var workbook = application.Workbooks.Open(fileName.Name);
var worksheet = workbook.Worksheets[1];
worksheet.SaveAs("myfile.csv", ",");
}
}
When I debug, the fileName is retrieved successfully, but the application breaks on using (ExcelEngine excelEngine = new ExcelEngine()) with a NullReferenceException error.
Where am I going wrong here?
The way you can do it is defined here: https://www.syncfusion.com/kb/9098/how-to-export-excel-data-to-csv-file
Most likely, you would need to install from NuGet not only Syncfusion.XlsIO.WinForms , but also System.Drawing.Common and System.Security.Permissions.
This code worked:
try
{
//Initialize ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Initialize Application
IApplication application = excelEngine.Excel;
//Set default version for application
application.DefaultVersion = ExcelVersion.Excel2013;
//Open a workbook to be export as CSV
IWorkbook workbook = application.Workbooks.Open(#"E:\Users\Public\Documents\" + "ExcelFile.xlsx");
//Accessing first worksheet in the workbook
IWorksheet worksheet = workbook.Worksheets[0];
//Save the workbook to csv format
worksheet.SaveAs("Output.csv", ",");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
Make sure you have permission to access the Excel file. Usually, it helps if you test it with files which are saved on another drive than on which Windows is installed (e.g. E:\, if Windows is on C:\).
Thank you, it turned out I was missing a reference to System.Drawing nuget
i'am trying to get an xls file from an ZipArchive but cant get it with EPPLUS
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry != null)
{
string filepath = entry.FullName;
FileInfo fileInfo = new FileInfo(filepath);
//here i got the excel package with the xls file inside the excelPackage
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
//but here impossible de get the worksheet or workbook inside or anything else
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault();
int totalColomn = worksheet.Dimension.End.Column;
int nbrsheet = excelPackage.Workbook.Worksheets.Count();
}
}
}
the ExcelPackage i get in debug
i see the xls file on debug inside the excelpackage but just when i try to get worksheet it exit without exception code....
same here when trying with entryStream
using (var entryStream = entry.Open())
{
//Cant even get the excelpackage, it crash here without exception
using (ExcelPackage excelPackage = new ExcelPackage(entryStream))
{
ExcelWorksheet worksheetest = excelPackage.Workbook.Worksheets.FirstOrDefault();
}
}
the stream here seem also strange ...
entryStream Debug
Working with .NET CORE Blazor ServerSide, ePPLUS 4.5
Thanks for helping
entry.FullName refers to the full path to the file inside the zip archive, while FileInfo describes a file in the filesystem of the OS, which is a completely different thing. You haven't extracted anything to the OS filesystem yet, so the FileInfo won't refer to a file that actually exists.
Try the ExcelPackage constructor that takes a Stream, which you can get directly from a ZipArchiveEntry:
using (var entryStream = entry.Open())
{
using (ExcelPackage excelPackage = new ExcelPackage(entryStream))
{
// ...
}
}
I find the problem.
it was that i tried to get an xls file and the epplus library dont work with it...
you have to be careful, EPplus dont work with xls file
So , your solution Jeff is working, it was my fault, didn't specified the extension of my excel file... sorry
-> EPlus with an .xlsx OK, not .xls
My bad.
Thanks anyway :-)
I'm using the latest 4.5.3.3 nuget version. .NET 4.6.1.
No issues if I create an excel file from scratch, but if I need to update an existing one, even if passing it to the ExcelPackage ctor and calling a ExcelPackage.Save after updating the data, or create a copy of the original excel file to pass to ExcelPackage ctor I obtain the same result.
FileInfo exc = new FileInfo(destPath);
using (var ep = new ExcelPackage(exc))
{
foreach (var sheet in _resultTranslations.Keys)
{
List<string> sheetTrs = _resultTranslations[sheet];
var ws = ep.Workbook.Worksheets[sheet];
if (ws == null)
continue;
for (int r = 0; r < sheetTrs.Count; r++)
{
ws.Cells[2 + r, 6].Value = sheetTrs[r];
}
}
ep.Save();
}
UPDATE:
If I open the Excel file with Office 2016, and save it without any changes, EEPlus updates it correctly (it's no more blank).
Any suggestion?
Thanks.
All I want to do is to create a file with 1 line and few columns to make some test.
However I cannot find a simple way to do such a thing. Most help I found seem to also use a database.
I found one that doesn't.
Trying to create a new .xlsx file using NPOI and write to it
However it seems like a little too much for such a simple task. Is there a tool which would allow me to do something as simple as
Create file
file.cell[0,0] = ...
You could use the EPPlus package. Find it on NuGet.
string fileName = #"C:\test\MyNewExcelFile.xlsx";
FileInfo newFile = new FileInfo(fileName);
using (ExcelPackage xlPackage = new ExcelPackage(newFile)) // create the xlsx file
{
// Add a new worksheet on which to put data
ExcelWorksheet xlWorksheet = xlPackage.Workbook.Worksheets.Add("Test");
// Write data to cell(s)
xlWorksheet.Cells["A1"] = ...
// Write the file
xlPackage.Save();
}
EDIT
Modification to replace worksheet if it already exists.
const string fileName = #"C:\test\MyNewExcelFile.xlsx";
const string sheetName = #"test";
FileInfo newFile = new FileInfo(fileName);
using (ExcelPackage xlPackage = new ExcelPackage(newFile)) // create the xlsx file
{
// if sheet already exists, delete it
// use LINQ to query collection of worksheets
ExcelWorksheet xlWorksheet = xlPackage.Workbook.Worksheets.SingleOrDefault(ws => ws.Name == sheetName);
if (xlWorksheet != null)
{
// worksheet exists - delete it
xlPackage.Workbook.Worksheets.Delete(sheetName);
}
// Add new worksheet on which to put data
ExcelWorksheet xlWorksheet = xlPackage.Workbook.Worksheets.Add("Test");
// Write data to cell(s)
xlWorksheet.Cells["A1"] = ...
// Write the file
xlPackage.Save();
}
I am trying to copy certain data from a sheet to another, but some cells are simple strings and some are hyperlinks.
If I use StringCellValue on the strings one it's ok, but I haven't found a method to copy the hyperlinks from the original sheet into the new one that I am constructing.
For the construction of the new sheet and for data copying I am using NPOI.
//UPDATE
I have added the code to insert the hyperlinks but when I run the program it shows the following exception: Object reference not set to an instance of an object.
Here is my code:
using (FileStream fs = new FileStream(#"C:\Users\File.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Console.WriteLine("Time to wait....");
templateWorkbook = new XSSFWorkbook(fs);
}
row.GetCell(6).SetCellValue(sheettoget.GetRow(1).GetCell(13).StringCellValue);
var sourceLink = sheettoget.GetRow(1).GetCell(13).Hyperlink;
if(sourceLink != null)
{
Console.WriteLine("Inserting first Juice session...");
var targetLink = new XSSFHyperlink(sourceLink.Type);
targetLink.Address = sourceLink.Address;
}
row.GetCell(6).Hyperlink = targetLink;
row.GetCell(6).CellStyle = sheettoget.GetRow(1).GetCell(13).CellStyle;
You can copy a hyperlink like this, where sourceCell is the cell you are copying from, and targetCell is the cell you are copying to:
targetCell.SetCellValue(sourceCell.StringCellValue);
var sourceLink = sourceCell.Hyperlink;
if (sourceLink != null)
{
var targetLink = new XSSFHyperlink(sourceLink.Type);
targetLink.Address = sourceLink.Address;
targetCell.Hyperlink = targetLink;
// also copy the cell style to ensure the copied link still looks like a link
targetCell.CellStyle = sourceCell.CellStyle;
}