Number of pages of excel sheet - c#

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!!!

Related

Excel formula attached to Column but value is not rendered

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.

How to read and import data from Excel to database using EEPLUS

I want to read data from excel using EEPLUS in C#. Who can teach me or share the link to teach it? Thanks for reading!
It is really easy to use, just read through the GetStarted of their GitHub.
Example:
//Open the workbook (or create it if it doesn't exist)
var fi=new FileInfo(#"c:\workbooks\myworkbook.xlsx")
using (var p = new ExcelPackage(fi))
{
//Get the Worksheet created in the previous codesample.
var ws=p.Workbook.Worksheets["MySheet"];
Set the cell value using row and column.
ws.Cells[2, 1].Value = "This is cell A2. It is set to bolds";
//The style object is used to access most cells formatting and styles.
ws.Cells[2, 1].Style.Font.Bold=true;
//Save and close the package.
p.Save();
}

Appending new cells breaks the excel file

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);
}

Does ClosedXML support setting a worksheet's zoom level?

I am using closedXML to generate an Excel file from C# and I am trying to see if there is anyway to set the zoom level of a worksheet. I can't find this in any of the documentation or from googling?
Does anybody know how to do this?
It's now possible, starting with ClosedXML version 0.87.0, via the IXLSheetView.ZoomScale property.
using Excel = ClosedXML.Excel;
var wb = new Excel.XLWorkbook();
Excel.IXLWorksheet ws = wb.AddWorksheet("zoom");
Excel.IXLSheetView view = ws.SheetView;
/* Value can be set between 10 and 400 */
view.ZoomScale = 85;
You can check the IXLSheetView source code for more information.
Update for version 0.87+: https://stackoverflow.com/a/52755386/2610249
No, ClosedXML does not support setting the zoom. The option that johny links to is only for scaling of the pages when printing.
There is a feature request on the ClosedXML page, but no answer from the developer.
As previously answered, you can't, but I've found a way around it which works well:
Create a template Excel file in advance, with all sheets' zoom levels set to how you want them.
When you create your workbook, instead of:
public XLWorkbook CreateWorkbook()
{
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.AddWorksheet("First sheet");
// ...
return workbook;
}
do this:
public XLWorkbook CreateWorkbookWithZoom()
{
XLWorkbook workbook = new XLWorkbook(#"C:\your template file.xlsx");
IXLWorksheet worksheet = workbook.Worksheet(1);
worksheet.Name = "First sheet";
// ...
return workbook;
}
where C:\your template file.xlsx is the path of your template file.
I think you can also handle having a variable number of sheets, by copying existing (blank) worksheets instead of creating new ones. You can get creative with having different blank template worksheets to choose from, if you need to set the zoom level dynamically.
A pull request for this has been logged at https://github.com/ClosedXML/ClosedXML/pull/180

Slow Performance When Reading Excel

I want to read excel file but in this way is too slow. What pattern should I use to read excel file faster. Should I try csv ?
I am using the following code:
ApplicationClass excelApp = excelApp = new ApplicationClass();
Workbook myWorkBook = excelApp.Workbooks.Open(#"C:\Users\OWNER\Desktop\Employees.xlsx");
Worksheet mySheet = (Worksheet)myWorkBook.Sheets["Sheet1"];
for (int row = 1; row <= mySheet.UsedRange.Rows.Count; row++)
{
for (int col = 1; col <= mySheet.UsedRange.Columns.Count; col++)
{
Range dataRange = (Range)mySheet.Cells[row, col];
Console.Write(String.Format(dataRange.Value2.ToString() + " "));
}
Console.WriteLine();
}
excelApp.Quit();
The reason your program is slow is because you are using Excel to open your Excel files. Whenever you are doing anything with the file you have to do a COM+ interop, which is extremely slow, as you have to pass memory across two different processes.
Microsoft has dropped support for reading .xlsx files using Excel interop. They released the OpenXML library specifically for this reason.
I suggest you use a wrapper library for using OpenXML, since the API is pretty hairy. You can check out this SO for how to use it correctly.
open xml reading from excel file
You're accessing Excel file through excel interop. By doing reads cell by cell you're doing a lot of P/Invoke's which is not very performant.
You can read data in ranges, not cell by cell. This loads the data into memory and you could iterate it much faster. (Eg. try to load column by column.)
BTW: You could use some library instead like http://epplus.codeplex.com which reads excel files directly.
Excel Data Reader
Lightweight and very fast if reading is your only concern.

Categories

Resources