Aspose Cells ExportDataTable CellsException - c#

I am trying to read an excel file of size 263 KB, it includes 3 columns and 6998 rows of data, the first column is alphanumeric id Site ID ex: 'D7302', and the other 2 columns are latitude and longitude of type decimal,
try
{
dataTable = worksheet.Cells.ExportDataTable(HeaderLineStartingRowNumber, 0, worksheet.Cells.MaxRow - HeaderLineStartingRowNumber + 1, worksheet.Cells.MaxColumn + 1, true);
}
catch (CellsException)
{
dataTable = worksheet.Cells.ExportDataTableAsString(HeaderLineStartingRowNumber, 0, worksheet.Cells.MaxRow - HeaderLineStartingRowNumber + 1, worksheet.Cells.MaxColumn + 1, true);
}
this code always throws CellsException and exports data as string which truncates decimals and returns a string so instead of 28.125027778 it returns 28, when i reduced the file size to 100 rows it was exported successfully as expected so it must be a size issue, so are there size limitations for ExportDataTable , Thank you

We have tested your issue with the following sample code and it worked fine. We tested this issue with the most recent version i.e. 18.4 uploaded on NuGet. Please download the sample Excel file used inside the code and also see the screenshot that shows, data from worksheet has been exported to data table successfully.
The code is exactly similar to yours with few changes so we do not find any error in your code. Therefore, if you are using the older version, you must use the most recent version.
If your issue still occurs with the most recent version, then provide us your runnable sample code (preferably sample console application project) along with all the files used inside your code. We will execute your code at our end and try to replicate the issue and log it in our database for a fix in future releases. Thanks for your cooperation in this regard and have a good day.
Sample Excel File used inside the Code - Download Link:
sampleExportDataTable.zip
Sample Code
Workbook wb = new Workbook("sampleExportDataTable.xlsx");
Worksheet worksheet = wb.Worksheets["Data"];
int HeaderLineStartingRowNumber = 25;
var dataTable = worksheet.Cells.ExportDataTable(HeaderLineStartingRowNumber, 0, worksheet.Cells.MaxDataRow - HeaderLineStartingRowNumber + 1, worksheet.Cells.MaxDataColumn + 1, true);
Screenshot:
Note: I am working as Developer Evangelist at Aspose

I had this error by this line code:
var dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.Rows.Count
, worksheet.Cells.Columns.Count, true);
Then change it to these codes and the error resolved:
(ExportTableOptions and ExportAsString)
ExportTableOptions options = new ExportTableOptions();
options.ExportAsString = true;
options.ExportColumnName = true;
var dataTable = worksheet.Cells.ExportDataTable(0, 0,worksheet.Cells.Rows.Count, worksheet.Cells.Columns.Count, options);

Related

removing rows in file based on condition

I am running a Postman test suite and the results of the tests are exported into a json file. I have successfully been able to convert the results into a excel file. However, is there a way for me to delete records that do not begin with a particular string? Below is an example of what I am seeing in the output file of three columns (I clearly changed this data for security reasons). I am only wanting to see rows that start with TC:
Get Token test.com(12345) 1
Submit SR test.com(12345) 1
TC - Testing test.com(12345) 1
Also if anyone knows how to change the third column to a string on each row, I will gladly take that knowledge sharing but the other piece is what I am trying to do right now. Not a high critical problem because I can easily just go into the excel file and sort out the rows but just seeing if there is an easier way to do it. Here is the code that I have so you can see I was able to get a file created...
using Aspose.Cells;
using Aspose.Cells.Utility;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.AspNet.SignalR.Json;
using Newtonsoft.Json;
// create a blank Workbook object
var workbook = new Aspose.Cells.Workbook();
var worksheet = workbook.Worksheets[0];
//Read json file
string jsonInput = File.ReadAllText(#"C:\Users\kruepb\Desktop\QA TEST RUN");
//set JsonLayoutOptions for formatting
JsonLayoutOptions options = new JsonLayoutOptions();
options.ArrayAsTable = true;
//Import JSON Data
Aspose.Cells.Utility.JsonUtility.ImportData(jsonInput, worksheet.Cells, 1, 0, options);
//Format sheet
worksheet.Cells.DeleteRows(0, 3);
worksheet.Cells.DeleteColumns(0, 9, false);
worksheet.Cells.DeleteColumn(2);
worksheet.Cells.DeleteColumns(3, 125, false);
//Add headers
worksheet.Cells["A1"].PutValue("Name");
worksheet.Cells["B1"].PutValue("URL");
worksheet.Cells["C1"].PutValue("Passed?");
worksheet.Cells["D1"].PutValue("Output");
// save CSV file
workbook.Save(#"C:\Users\kruepb\Desktop\TESTRUN.csv");

EPPlus InsertRow corrupts excel file

I am currently comparing different files with each other and put the output in an Excel Workbook with multiple worksheets (one per file comparison) using EPPlus 4.5.3.3.
The worksheets are simple ranges - no tables.
Sometimes it is required to move the content of one Excel Sheet to a certain row in another Excel Sheet.
For this I am using the InsertRow function of EPPlus. The function itself does the trick and inserts the content at the desired row.
However, when opening the Workbook I receive these errors:
The content of that XML file is the following:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error044200_02.xml</logFileName>
<summary>Errors were detected in file 'C:\Users\...\test.xlsx'</summary>
-<repairedParts>
<repairedPart>Repaired Part: /xl/worksheets/sheet5.xml part with XML error. Load error. Line 1, column 0.</repairedPart>
</repairedParts>
When I am adding the content at the last row without using InsertRow everything is working fine.
Also, if I save the ExcelPackage immediately after using InsertRow the error is also present, so it definitely has something to do with this function (also if I am only using one Worksheet). After using InsertRow it seems like the whole formatting of the respective Excel Sheet is screwed up. The inserted content appears to be right though.
The excel is initially created from an empty template excel that contains nothing but the header names (also had the same issue with a template without anything inside):
The following code can be used to reproduce (this is just an example - in my case every sheet has more than 50 rows):
FileInfo filePath= new FileInfo(fileName);
excel = new ExcelPackage(filePath);
ExcelWorkbook wb = excel.Workbook;
foreach (ExcelWorksheet ws in wb.Worksheets)
{
int i = 5;
ws.InsertRow(i, 1);
//same effect with ws.InsertRows(i, 1, i-1)
ws.Cells[i, 1].Value = "test";
ws.Cells[i, 2].Value = "test";
//excel.Save();
}
excel.Save();
Although my answer is not based on EPPlus, have you looked at this post ?
Inserting new rows and moving exsisting ones with OpenXML SDK 2.0
I am guessing that you are getting errors because the newly added rows from the ws.InsertRow function are not updating the required row indexes. This is mentioned in the post.
Also there is a recommendation in the comments "To anyone who is getting an error when opening the file try this line before you save your document'... xlsFile.WorkbookPart.DeletePart(xlsFile.WorkbookPart.CalculationChainPart);

EPPlus Shows System Argument Exception

I am having issue reading in some worksheet from a workbook using C# and EPPlus.
Error Message
name contains invalid characters
Even after multiple checks on the worksheet name, file name and column headers within the workbook to make sure there is no invalid characters, I am still facing the same error.
However, when I move the worksheet that i need from that same workbook to a new Excel file and save it with the same file name and worksheet name, I have no error while reading in.
Does anybody know how to solve or even encounter this problem?
Code
using (ExcelPackage xlPackage = new ExcelPackage(mStream))
{
//WorksheetId = 5
var ws = xlPackage.Workbook.Worksheets[WorksheetId]; //This is the part that is causing error
for (int i = 1; i <= ws.Dimension.End.Column; i++)
{
dt2.Columns.Add((i - 1).ToString());
}
}
Came across this LINK and realized this is actually a bug in EPPlus for version 4.5.2.2 onward.
After reverting back to the older version, everything works fine.
There is a change the "name" as mentioned in the error is not the sheet name.. There is a section in the Formular tab of the Excel software called "Name Manager".
There you will find list of names and references there. The error above per the source code of EPPlus is generated when EPPlus tries to validate the name range.
I had to modify the EPPlus source code to ignore the error since the file i was dealing with was locked and needed to be preserved as it was.

C# Excel Interop row limit -> HRESULT: 0x800A03EC exception thrown

I have a C# application which purpose is to store a big amount of data. I am using Microsoft.Office.Interop.Excel (Microsoft.Office.Interop.Excel.dll Version 14.0.0.0) to help me accomplish this. I have Excel 2007 installed.
I use the following lines:
excelApp = new Microsoft.Office.Interop.Excel.Application();
excelWorkBook = excelApp.Workbooks.Add(misValue);//*--------> LINE NOT WORKING */
excelWorksheetBeingWritten = (Excel.Worksheet)excelWorkBook.Worksheets.get_Item(1);
My code then iterates through a big list of objects, and each time a row must be written I do something like:
var startCell = excelWorksheetBeingWritten.Cells[excelLineCounter, 1];
var endCell = excelWorksheetBeingWritten.Cells[excelLineCounter, 2];
string[] tmpArray = new string[2] { stringVar1, stringVar2 };
tmpRange = excelWorksheetBeingWritten.Range[startCell, endCell];
tmpRange.Value = tmpArray;
When excelLineCounter exceeds 65536, the "HRESULT: 0x800A03EC exception" is thrown. I am perfectly aware of the (in)famous pre-Excel2007 row limit (which is precisely 65536). What I don't understand is why the interops are using that limit, when Excel 2007 (my version) has a documented limit of 1.048.576 rows.
On the other hand, if I replace the above "LINE NOT WORKING" by the following, it seems to use the Excel 2007 row limit, and the exception vanishes:
excelWorkBook = excelApp.Workbooks.Open(#"H:\Workbook1.xlsx");//*--------> LINE WORKING */
Note: "Workbook1.xlsx" is an empty workbook previously saved as "Excel Workbook (*.xlsx)"
Can someone please tell me what kind of sorcery do I need to do in order to configure the Excel Interop objects to use the Excel 2007 limits by default, preferably without having a previously saved empty .xlsx file laying around?
I encountered a similar issue yesterday and the solution is to change your Excel settings to create xlsx files by default.
In Excel: File -> Options -> Save -> Save files in this format
Your default is probably 'Excel 97-2003 (*.xls)' like mine was. If you change it to 'Excel Workbook (*.xlsx)', your code will work.

Microsoft Open SDK 2.0 to generate excel file using c#

Please refer link for the code i am using to generate excel file from data table. I am able to generate excel file successfully.
But the problem/Challenge/Question is as follows.
I Want to generate column as per the datatype so if Column value contains date the it cell format should be date(dd/mm/yyy) if number then numeric. ans so on...
I have tried to generate excel file as per data format you can see specific methods to generate cell value. But the problem is when user download the file it will gives the warning message that "Excel found unreadable content 'filename'. Do you want to recover the content of this workbook?". I don,t want that warning message should come.
If I am writing everything as text without format then file will open without any warning message and after downloading file, if user tries to format respective column in date or number format then also it will not allow user to format/slice & dice data in excel file.
Reference :- http://www.codeproject.com/Articles/263106/Export-Tabular-Data-in-CSV-and-Excel-Formats-Throu
Please let me know the solution if anybody has.
I am using DocumentFormat.OpenXml.dll
private Cell CreateTextCell(string header, UInt32 index, string text)
{
var cell = new Cell {DataType = CellValues.InlineString, CellReference = header + index};
var istring = new InlineString();
var t = new Text {Text = text};
istring.Append(t);
cell.Append(istring);
return cell;
}
private Cell CreateDateCell(string header, UInt32 index, DateTime sDate)
{
Cell cell = new Cell();
cell.DataType = CellValues.Date;
cell.CellReference = header + index;
cell.StyleIndex = UInt32Value.FromUInt32(14);
cell.CellValue = new CellValue { Text = sDate.ToOADate().ToString() };
return cell;
}
private Cell CreateNumberCell(string header, UInt32 index, string text)
{
Cell cell = new Cell();
cell.DataType = CellValues.Number;
cell.CellReference = header + index;
cell.CellValue = new CellValue(text);
return cell;
}
I have moved to EPPlus .net library to generate excel file and it is very easy to use.
Thanks.
I may not have the complete solution but you can try the below steps to find the root cause:
One of the reasons for unreadable content error (There are many reasons for this but considering that you are just writing from the datatable only).is if there is a mismatch between the StyleIndex/Datatype/Cellvalue format
To identify the root cause you can try:
Create a sample excel and directly write a cell (only a cell) with number, it that goes on fine, try the next datatype one by one.
Next try to write them one after the other.
Do this and sort down the types which are causing problems.
Next try to vary the StyleIndex/cellFormat with that type (you can check for examples online) till the format is fine without error. Once you are good with all types, you can try writing to the entire excel.
You can also use this method if you want to try something new using OpenXML (since you do not have well documented examples for everything Online)
Another way to identify issues is to use Openxml productivity tool and do a validation of the file.

Categories