i have been trying to download an excel workbook from windows application(C#) with data from DB. i have few sheets that have static data and few sheets formulated with data from DB. There are few column where i have formatted excel formulas.
When i download the sheet that contains only DB data, the formulas columns have accurate value rendered in them. But while downloading with Static Data Sheets, the formulas is bind with columns but value is not rendered. On clicking on any cell for edit in the sheet,the values are rendered.
i am using NPOI dll. For adding the static data sheets, i have been initializing the excel to be downloaded with static field sheets and adding the remaining sheets.
IWorkbook hssfwb;
ISheet sheetObj;
//--- read existing data
using (FileStream file = new FileStream(Existingfile, FileMode.Open, FileAccess.Read))
{
hssfwb = new XSSFWorkbook(file);
file.Close();
}
//-- adding sheets from DB data
sheetObj = (XSSFSheet)hssfwb.CreateSheet(dtSet.Tables[sheetcount].TableName);
I've faced the click and display problem before.
There is a ForceFormulaRecalculation property on ISheet, you can try to set it to true.
Related
Does anyone know how to get the number of total pages within a excel sheet via OpenXml? I do NOT need the number of sheets. I need the number of pages within a sheet.
Background:
We have to export multiple worksheets into a pdf file. Therefore we have to create a table of content.
Thanks!
EDIT-------
This is how it can be achieved via Office Interop:
var app = new MsExcel.Application();
var wb = app.Workbooks.Add(fullPath);
foreach (Worksheet sheet in wb.Sheets)
{
sheet.Activate();
string name = sheet.Name;
int pages = sheet.PageSetup.Pages.Count;
}
It would be gread if someone knew a solution with OpenXml, ClosedXml or any other free library.
Thanks again!!!
I need to download the Excel report upon generating it using .NET c# EP Plus.
I tried using auto filter, pivoting the table to extract values from DB, but I couldn't reach the solution.
MemoryStream ms = new MemoryStream();
ExcelPackage pck = new ExcelPackage();
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("PTcF_Summary");
ws.Cells["A1"].LoadFromDataTable(dt, true);
var pivotTable = ws.PivotTables[0]; //assuming that we have only have 1
string pivotField = pivotTable.Fields["Type"].ToString();
//pivotTable.PageFields.Add(pivotField);
var validation = ws.DataValidations.AddListValidation("G2");
validation.Formula.Values.Add(pivotField);
ws.Cells["G2"].AutoFilter = true;
I expect the output as one cell in the excel should be a drop down showing the list of options through which I can filter the data of table in Excel. The filter should work like, on selecting a drop down value, if a particular column has it, then the respective row should be displayed and others should be filtered out. And upon selecting another drop down value, the same should happen vise versa.
This might be the duplicate as many posts refer to this kind of issues but I could not find an exact answer to this case.
So, what I have is an Excel file where cell "E4" contains a formula "=C4+D4". All other cells are empty, which means I cannot search or get them via OpenXml, simply because they do not exist in Xml. So in order to fill the cells "C4" and "D4" I have to create them (like this:)
var cell = new Cell(){
CellReference = new StringValue("C4"),
DataType = new EnumValue<CellValues>(CellValues.Number),
CellValue = new CellValue("123")
}
the same for cell "D4" and then append these cells to the row
row.Append(cell);
After I open the excel file it show an error "Excel found unreadable content in file.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
I checked and when there is no formula in excel file the cells are appended correctly.
So my question is, using OpenXml how do I append cells in Excel file, which contains formula, so that not to break the file?
So the XML is malformed and corrupted the has file, hence the error:
Excel found unreadable content in file
To troubleshoot and fix this I suggest you compare the manually created xlsx to the programmatically created xlsx.
To do this you rename both files extensions from XLSX to ZIP > extract them to different folders > use a WinDiff tool (or better the "OpenXML SDK Productivity Tool") to compare the files.
Once you find how its malformed change your code to try and fix it up.
I solved my problem using EPPLUS
using (ExcelPackage excelPackage = new ExcelPackage(fileStream))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells["C4"].Value = "123";
excelWorksheet.Cells["D4"].Value = "321";
excelPackage.SaveAs(memoryStream);
}
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();
I'm using the NPOI Library to import some data from an excel sheet to my database.
One early issue I'm curious about, is typically, to begin, you would have something like this:
snippet
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
{
HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs);
HSSFSheet sheet = templateWorkbook.GetSheet("Sales");
My query is, what happens when the name of the worksheet is different each time, e.g. I plan to import sales data, and the sheets within the workbooks I receive from suppliers are titled by date.
Is there any way that you can specify GetSheet(); to just get the first sheet in the workbook?
Any pointers would be much appreciated,
thanks guys :)
Without trying it out I'd assume that you're looking for getSheetAt.
So it would be:
HSSFSheet sheet = templateWorkbook.GetSheetAt(0);