测试模板
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "";
ofd.Filter = "文件|*.*";
ofd.Multiselect = true;
string[] files;//数据源路径集合
DialogResult r = ofd.ShowDialog();
if (r == DialogResult.OK)
{
files = ofd.FileNames;
}
else
{
return;
}
foreach (var file in files)
{
//此处添加测试代码
}
Console.ReadLine();
读取EXCEL
NuGet中导入NPOI
IWorkbook workbook = null;
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
string extension = Path.GetExtension(file).ToUpper();
switch (extension)
{
//xls是03,用HSSFWorkbook打开,.xlsx是07或者10用XSSFWorkbook打开
case ".XLS": workbook = new HSSFWorkbook(fs); break;
case ".XLSX": workbook = new XSSFWorkbook(fs); break;
default: continue;
}
for (int index = 0; index < workbook.NumberOfSheets; index++)
{
ISheet sheet = workbook.GetSheetAt(index);
for (int i = sheet.FirstRowNum; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row == null) break;
for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
{
ICell cell = row.GetCell(j);//得到cell
if (cell != null)//如果cell为null,则赋值为空
{
string context = row.GetCell(j).ToString();
Console.Write(context + " ");
}
}
Console.Write("\n");
}
}
读取PPT
NuGet导入Spire.Presentation
//初始化一个Presentation类实例,并加载文档
Presentation ppt = new Presentation();
try
{
ppt.LoadFromFile(file);
foreach (ISlide slide in ppt.Slides)
{
foreach (Spire.Presentation.IShape shape in slide.Shapes)
{
if (shape is IAutoShape)
{
foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
{
Console.WriteLine(tp.Text);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//释放资源
ppt = null;
}
读取Word
导入DotMaysWind.Office.dll,来源GitHub项目Simple Office Reader
var document = OfficeFileFactory.CreateOfficeFile(file) as IWordFile;
string context = document.ParagraphText;
Console.WriteLine(context);
读取PDF(非扫描版)
NuGet导入Spire.Pdf
PdfDocument pdf = new PdfDocument();
try
{
pdf.LoadFromFile(file);
foreach (PdfPageBase page in pdf.Pages)
{
string context = page.ExtractText();
Console.WriteLine(context);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//释放资源
pdf = null;
}