Excel Reader c# (E+15 Issue) - excel

I load my excel to gridview in c# Winform and I use Excel.4.5 for reading.My value in my excel row cell 7885266361743930.But it comes to my gridview like this => 7,88526636174393E+15 in normal what I waiting
is => 7885266361743930 like in my excel :)
This is my method to read the excel :
var stream = File.Open(openFileDialog1.InitialDirectory + openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

Related

Excel and Libre Office conflict over Open XML output

Open XML is generating .xlsx files that can be read by Open Office, but not by Excel itself.
With this as my starting point( Export DataTable to Excel with Open Xml SDK in c#) I have added code to create a .xlsx file. Attempting to open with Excel, I'm asked if I want to repair the file. Saying yes gets "The workbook cannot be opened or repaired by Microsoft Excel because it's corrupt." After many hours of trying to jiggle the data from my table to make this work, I finally threw up my hands in despair and made a spreadsheet with a single number in the first cell.
Still corrupt.
Renaming it to .zip and exploring shows intact .xml files. On a whim, I took a legit .xlsx file created by Excel, unzipped it, rezipped without changing contents and renamed back to .xlsx. Excel declared it corrupt. So this is clearly not a content issue, but file a format issue. Giving up on Friday, I sent some of the sample files home and opened them there with Libre Office. There were no issues at all. File content was correct and Calc had no problem. I'm using Excel for Office 365, 32 bit.
// ignore the bits (var list) that get data from the database. I've reduced this to just the output of a single header line
List< ReportFilingHistoryModel> list = DB.Reports.Report.GetReportClientsFullHistoryFiltered<ReportFilingHistoryModel>(search, client, report, signature);
MemoryStream memStream = new MemoryStream();
using (SpreadsheetDocument workbook = SpreadsheetDocument.Create(memStream, SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new Workbook();
workbook.WorkbookPart.Workbook.Sheets = new Sheets();
var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
sheetPart.Worksheet = new Worksheet(sheetData);
Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "History" };
sheets.Append(sheet);
Row headerRow = new Row();
foreach( var s in "Foo|Bar".Split('|'))
{
var cell = new Cell();
cell.DataType = CellValues.Number;
cell.CellValue = new CellValue("5");
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
}
memStream.Seek(0, SeekOrigin.Begin);
Guid result = DB.Reports.Report.AddClientHistoryList( "test.xlsx", memStream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return Ok(result);
This should just work. I've noticed other stack overflow discussions that direct back to the first link I mentioned above. I seem to be doing it right (and Calc concurs). There have been discussions of shared strings and whatnot, but by using plain numbers I shouldn't be having issues. What am I missing here?
In working on this, I went with the notion that some extraneous junk on the end of a .zip file is harmless. 7-Zip, Windows Explorer and Libre Office all seem to agree (as does some other zip program I used at home whose name escapes me). Excel, however, does not. Using the pointer at memStream.GetBuffer() was fine, but using its length was not. (The preceding Seek() was unnecessary.) Limiting the write of the data to a length equal to the current output position keeps Excel from going off the rails.

Openxml to write to excel not wrapping textbox value

I am using Openxmlto write to excel uisng C#
It is writing in excel but if data exceeds textbox length, it is not wrapping up and coming to second line. Attaching screenshot for same.
How to wrap the same ?
ExcelOxml.SetCellValue(document, worksheet1, 5, 89, pricingmodel.PricingDetailExtended.PricingProductDetail.OpportunityId, false);
Please use the below code to wrap the text in excel
//Create a cell format
CellFormat cellformat1 = new CellFormat(new Alignment() { WrapText=true});
CellFormats cellformats = new CellFormats();
cellformats.Append(cellformat1);
workbookstylesheet.Append(cellformats);
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();

chart formatting lost when save as xlsx in NPOI

I have a template xlsx, called "book3.xlsx" that contains a pre-formatted chart. All I want to do is to read my template xlsx and modify some cells and write out the a xlsx ("test.xlsx")
A strange thing happens here, if i open the result xlsx the format of the chart has changed... It has different coloring, axis is missing etc. When I used xls file format it was fine, but we should use xlsx. Anyone noticed this issue?
var file = new FileStream(#"book3.xlsx", FileMode.Open, FileAccess.Read);
var workbook = new XSSFWorkbook(file);
using (var fs = new FileStream(#"test.xlsx", FileMode.Create))
{
workbook.Write(fs);
}

NPOI Multiple Sheets Error

I am trying to create an EXCEL XLSX file in a .net webcontext.
I tried this code but as soon as I add more than 1 sheet my excel becomes corrupt.
I am using NPOI 2.1.3
XSSFWorkbook wb = new XSSFWorkbook();
wb.CreateSheet("sheet1");
wb.CreateSheet("sheet 2);
MemoryStream memoryStream = new MemoryStream();
wb.Write(memoryStream);
memoryStream.Close();
var buffer = memoryStream.GetBuffer();
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.BinaryWrite(templateOutput);
When I use a FileStream the functionality works. (Multiple sheets)

Read and Write data in the same EXCEL - No Third Party

I am working in C#.Net. I will send a EXCEL File as my input and from that file i will do a process and will get the result.
I want to print the result in the same EXCEL sheet. The below code i am using to read excel.
public DataTable ReadDataExcel(string filepath)
{
FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
DataTable dt = new DataTable();
dt = result.Tables[0];
return dt;
}
I will use this datatable for process. How to print the OUTPUT Values in the same EXCEL Sheet. Without any third party's i need to do this..

Resources