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");
Related
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);
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);
So I have some code that creates an excel worksheet from Smartsheets and then extracts that information from the excel worksheet into SQL. Thing is the code works if I run each part seperately but if I run it at the same time it will create the table, but it won't input the data. Below I will show each part in the main program. I don't think it is necessary to show the rest of the code because as I say it works seperately.
//Make an Excel sheet from smartsheet
Smartsheet smartsheet = new Smartsheet();
long excelSmartsheetID = smartsheet.getSmartSheetID(currentWorkSheet);
smartsheet.createExcel(excelSmartsheetID);
//Extract Data From Excel into SQL
SSIS excelToSQL = new SSIS();
excelToSQL.storeSmartSheetDataToSQL();
So I am not sure what is going on here. I have put the thread to sleep for 10 seconds in between the two sections but it still doesn't work. Completely lost as to what might be the problem. I should add I am using SSIS to connect to the excel sheet and create it in SQL. Please let me know if you require more information.
It sounds like the Excel file is still open when trying to access it a second time. When an Excel file is open for writing it becomes locked which can prevent another process (in your case SSIS) from editing the file. This can be confirmed using the Smartsheet C# SDK with code like the following which never closes the file after writing to it.
// Set the Access Token
Token token = new Token();
token.AccessToken = "YOUR_TOKEN";
// Use the Smartsheet Builder to create a Smartsheet
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
BinaryWriter output = new BinaryWriter(new FileStream("c:\\file.xls", FileMode.Create));
smartsheet.Sheets().GetSheetAsExcel(8325033727682436L, output);
Console.WriteLine("Done writting");
System.Threading.Thread.Sleep(100000);
Run the above code and after it opens and writes to the file it will sleep for a very long time. While the code is sleeping you can try to manually open the Excel file and you will get a dialog like the following showing that we still have the Excel file open (from our code) even though we finished writing to it.
The solution to this issue is to close the Excel file as soon as we are done writing to it. This can be accomplished by using the close() method on the stream or with the using statement. I prefer the using statement so an example of that is below:
// Set the Access Token
Token token = new Token();
token.AccessToken = "YOUR_TOKEN";
// Use the Smartsheet Builder to create a Smartsheet
SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(token.AccessToken).Build();
using (BinaryWriter output = new BinaryWriter(new FileStream("c:\\file.xls", FileMode.Create)))
{
smartsheet.Sheets().GetSheetAsExcel(8325033727682436L, output);
}
Console.WriteLine("Done writting");
System.Threading.Thread.Sleep(100000);
Now if we run the above code it will sleep at the end but this time the Excel file will not be locked since the using statement closed the file as soon as we finished writing to it.
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.
How to create and download excel document using asp.net ?
The purpose is to use xml, linq or whatever to send an excel document to a customer via a browser.
Edit : Use case
The customer load a gridview ( made with ajax framework ) in a browser, the gridview is directly linked to an sql database.
I put a button 'export to excel' to let customer save this gridview data on his computer ansd i would like to launch a clean download of an excel.
The solutions proposed here are not clean, like send an html document and change the header to excel document etc, i'm searching a simple solution on codeplex right now, i will let you know.
Starter kit
First i have downloaded the Open XML Format SDK 2.0.
It comes with 3 useful tools in :
C:\Program Files\Open XML Format SDK\V2.0\tools
DocumentReflector.exe wich auto
generate the c# to build a
spreadsheet from the code.
OpenXmlClassesExplorer.exe display
Ecma specification and the class
documentation (using an MSDN style
format).
OpenXmlDiff.exe graphically compare
two Open XML files and search for
errors.
I suggest anyone who begin to rename .xlsx to .zip, so you can see the XML files who drive our spreadsheet ( for the example our sheets are in "xl\worksheets" ).
The code
Disclaimer : I have stolen all the code from an MSDN technical article ;D
The following code use an *.xlsx template i made manually to be able to modify it.
Namespaces references
using System.IO;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
// Database object
DataClassesDataContext db = new DataClassesDataContext();
// Make a copy of the template file.
File.Copy(#"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\livreurs.xlsx", #"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true);
// Open the copied template workbook.
using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(#"C:\inetpub\wwwroot\project.Web\Clients\Handlers\oxml-tpl\generated.xlsx", true))
{
// Access the main Workbook part, which contains all references.
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
// Get the first worksheet.
WorksheetPart worksheetPart = workbookPart.WorksheetParts.ElementAt(2);
// The SheetData object will contain all the data.
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
// Begining Row pointer
int index = 2;
// Database results
var query = from t in db.Clients select t;
// For each item in the database, add a Row to SheetData.
foreach (var item in query)
{
// Cell related variable
string Nom = item.Nom;
// New Row
Row row = new Row();
row.RowIndex = (UInt32)index;
// New Cell
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
// Column A1, 2, 3 ... and so on
cell.CellReference = "A"+index;
// Create Text object
Text t = new Text();
t.Text = Nom;
// Append Text to InlineString object
InlineString inlineString = new InlineString();
inlineString.AppendChild(t);
// Append InlineString to Cell
cell.AppendChild(inlineString);
// Append Cell to Row
row.AppendChild(cell);
// Append Row to SheetData
sheetData.AppendChild(row);
// increase row pointer
index++;
}
// save
worksheetPart.Worksheet.Save();
}
I havent finished yet, my second job is to auto download the spreadsheet after modification.
Finally, i redirect the user to my generated spredsheet (from my aspx)
context.Response.Redirect("Oxml-tpl/generated.xlsx");
just set Response.ContentType = "application/vnd.ms-excel" and your page will rendered as an excel sheet on the clients browser
Sample code here
There are quite a few ways of handling this, depending on how extensive the Excel functionality is. Binoj's answer works if the Excel is just a spreadsheet and has no direct Excel functionality built in. The client can add functionality, concats, etc. These are "dumb" excel docs until the client does soemthing.
To create a more full featured Excel doc, you havve two basic choices that I can think of offhand.
Use either the office components (re: bad) to create an excel document, or a third party component, like SoftArtisan's ExcelWriter. Great component, but there is a cost.
Use a control on the page that allows export to Excel. Most vendors of ASSP.NET controls have this functionality on their grids.
Option #1 allows you pretty much all functionality of Excel. Option #2 is a bit more limited, at least in the controls I have tried.
Good article on how top export to excel from Erika Ehrli
http://blogs.msdn.com/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx