using Microsoft.Office.Interop.Word;//需要导入的命名空间,需要从NuGet程序包里下载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculatePageFactory
{
public class CalculateWordPage
{
private static Microsoft.Office.Interop.Word.ApplicationClass myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
static object oMissing = System.Reflection.Missing.Value;
///
/// 得到Word文件的页数
///
/// word文件的路径
///
public static int GetWordPageCount(string path)
{
try
{
myWordApp.Visible = false;
object filePath = path; //这里是Word文件的路径
//打开文件
Microsoft.Office.Interop.Word.Document myWordDoc = myWordApp.Documents.Open(
ref filePath, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//下面是取得打开文件的页数
int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref oMissing);
myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
return pages;
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
using Spire.Pdf;//引入命名空间,没有的话去NuGet包里下载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculatePageFactory
{
public class CalculatePDFPage
{
///
/// 得到PDF文件的页数
///
/// 文件路径
///
public static int GetPDFPageCount(string filePath)
{
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.LoadFromFile(filePath);
//获取PDF页数
int page = pdf.Pages.Count;
return page;
}
}
}
1.先把Excle进行转换PDF格式的文档
using Microsoft.Office.Interop.Excel;//引入的命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchType
{
public class SwitchPDFType
{
//Microsoft.Office.Interop.Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
//Microsoft.Office.Interop.Excel.XlFixedFormatType excelType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
///
/// 将excel文档转换成PDF格式
///
/// 需要转换的文件路径
/// 转换好的之后 需要保存的文件路径
/// 这是个枚举类型
///
public bool Convert(string sourcePath, string targetPath, XlFixedFormatType targetType)
{
bool result;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application application = null;
Workbook workBook = null;
try
{
application = new Microsoft.Office.Interop.Excel.Application();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}
2.再将转换好的pdf文件计算器页数就行
public class CalatewitchPage
{
///
/// 处理转换为PDF的文件
///
/// 上传的Excle的文件路径
///
///
public static string GetSwitchedPDF(string filePath, string[] type)
{
SwitchPDFType switchPDF = new SwitchPDFType();
int excle = 0;
string newFileName = null;
StringBuilder sb = new StringBuilder();
Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = (Microsoft.Office.Interop.Excel.XlFixedFormatType)excle;
bool result = switchPDF.Convert(filePath, filePath, targetType);//调用Excle的方法
//如果转换成功
if (result)
{
string[] newType = new string[type.Length + 1];
for (int i = 0; i < newType.Length - 1; i++)
{
newType[i] = type[i] + ".";
}
newType[newType.Length - 1] = "pdf";
for (int i = 0; i < newType.Length; i++)
{
sb.Append(newType[i]);
}
}
newFileName = sb.ToString();
return newFileName;//得到转换后的文件名
}
}
//3.得到转换后的文件路径
string newFileName = CalatewitchPage.GetSwitchedPDF(filePath, type);
filePath = HttpContext.Current.Server.MapPath("~/file/" + newFileName);//转换后的文件路径
//4.调用计算pdf的页数就行了,代码就不写了,文章最上面有pdf页数的计算方法,直接把路径传过去就行了
如果有哪位大神能直接不用转换直接计算Excle页数的,请赐教!