views:

338

answers:

2

I am using DSOFile.OleDocumentPropertiesClass for getting the page count for office documents without automation. This works fine for docx and pptx files but returns always 0 for xlsx files.

DSOFile.OleDocumentPropertiesClass oleDocument = new DSOFile.OleDocumentPropertiesClass();
oleDocument.Open(documentFilePath, true, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);

//WORKS FOR DOCX
int pageCount = oleDocument.SummaryProperties.PageCount;


//WORKS FOR PPTS
int pageCount = oleDocument.SummaryProperties.SheetCount;

//NONE OF ABOVE WORKS FOR XLSX, IT ALWAYS RETURNS 0
A: 

well, DSOFile doesn't work for me either, as an alternative you can use OleDB to connect to an xls(x) file and get its content details. Below is an example:

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\1.xlsx;Extended Properties=Excel 8.0");
connection.Open();
DataTable dataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

if (dataTable == null) return;

foreach (DataColumn column in dataTable.Columns)
  Console.Write(column.ColumnName + '\t');
Console.Write('\n');
foreach (DataRow row in dataTable.Rows)
{
  foreach (DataColumn column in dataTable.Columns)
    Console.Write(row[column].ToString() + '\t');
  Console.Write('\n');
}

just make sure you have the Office 2007 system driver (Microsoft.ACE.OLEDB.12.0 oledb provider) installed

if you're dealing only with Excel 2007 spreadsheets you can consider using ExcelPackage (http://excelpackage.codeplex.com/) tool, it should be able to read data from the excel spreadsheet without automation

serge_gubenko
The OleDB solution works fine for read/write excel files. but here i want the page count when the document gets printed from a soft printer. this will not work for me. I am trying to avoid the automationProblem:User should be able to upload any type of document(doc,docx,ppt,pptx, xls,xlsx,images,pdf) and it will get converted to swf through a process, and finally that swf is used in application.Now we have a situation where we want to prohibit user to upload multiple page document. so we are using dso file it works fine for other formats remaining xls and xlsx.
Lalit
take a look at this link http://npoi.codeplex.com/, this thing might help you with what you're trying to do. Also I don't think you would be able to get the number of pages from xls, more likely a number of sheets. Pagination for each sheet is done on the fly before printing. With automation you can do it by calling ExecuteExcel4Macro with GET.DOCUMENT(50,\"your_sheet_name\") request
serge_gubenko
A: 

I have used automation for excel files and dso file for other formats, As i can't fild any solution regarding it.

Lalit