.net 文件预览

代码展示:

//获取文件路径

UploadFile uploadFiless = IoCContainer.Get<IUploadFileDao>().SelectByIdReq(uploadFile.ToInt32OrDefault());
string docAbsPath = UploadFileHelper.GetAbsPath(uploadFiless.Guid);
if (uploadFiless.Extension == ".doc" || uploadFiless.Extension == ".docx")
{
ViewWordAsImageByPath(docAbsPath);
}

else if (uploadFiless.Extension == ".xlsx" || uploadFiless.Extension == ".xls")
{
ViewExcelAssHtmlByPath(docAbsPath);
}

else if (uploadFiless.Extension == ".pdf")
{
ViewPdfAsHtmlByPath(docAbsPath);
}

else if (uploadFiless.Extension == ".png" || uploadFiless.Extension == ".jpg" || uploadFiless.Extension == ".jpeg" || uploadFiless.Extension == ".JPG")
{
ViewPictureAsHtmlByPath(docAbsPath);
}

else if (uploadFiless.Extension == ".txt")
{

ViewTextAsHtmlByPath(docAbsPath);
}

public void ViewWordAsImageByPath(string docAbsPath)
{
try
{
Document document = new Document(docAbsPath);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.PageCount = 1;
StringBuilder sb = new StringBuilder();
sb.Append(@"<div style=""text-align: center;"">");
for (int i = 0; i < document.PageCount; i++)
{
options.PageIndex = i;
string tempFileAbsPath, tempFileUrlPath;
TempFileHelper.CrateTempFilePath(".png", out tempFileAbsPath, out tempFileUrlPath);
document.Save(tempFileAbsPath, options);
sb.Append(string.Format(@"<img src=""{0}"" style="" width:auto; border: 1px solid black;""><br />", Nav.AddHost(tempFileUrlPath)));
}
sb.Append("</div>");
ViewWordHtml = sb.ToString();
}
catch (Exception ex)
{
ViewWordHtml = "请下载查看文件是否有破损";
}
}

public void ViewExcelAssHtmlByPath(string docAbsPath)
{
try
{
Workbook workbook = new Workbook(docAbsPath);
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet wsMoreTwo = workbook.Worksheets[i];
//SetColumnWithAuto(wsMoreTwo);
}
string tempFileAbsPath, tempFileUrlPath;
TempFileHelper.CrateTempFilePath(".Html", out tempFileAbsPath, out tempFileUrlPath);

workbook.Save(tempFileAbsPath, Aspose.Cells.SaveFormat.Html);
ViewWordHtml = string.Format(@"<iframe src=""{0}"" style=""width: 100%;Height:100%""></iframe>"
, Nav.AddHost(tempFileUrlPath));
}
catch (Exception ex)
{
ViewWordHtml = "请下载查看文件是否有破损";
}
}
/// <summary>
/// 设置表页的列宽度自适应
/// </summary>
/// <param name="sheet">worksheet对象</param>
private void SetColumnWithAuto(Worksheet sheet)
{
Aspose.Cells.Cells cells = sheet.Cells;
int columnCount = cells.MaxColumn; //获取表页的最大列数
int rowCount = cells.MaxRow; //获取表页的最大行数

扫描二维码关注公众号,回复: 7462404 查看本文章

for (int col = 0; col < columnCount; col++)
{
sheet.AutoFitColumn(col, 0, rowCount);
}
for (int col = 0; col < columnCount; col++)
{
cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);
}
}
public void ViePPTAsHtmlByPath(string docAbsPath)
{
try
{
Microsoft.Office.Interop.PowerPoint.Application application =
new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = application.Presentations.Open(docAbsPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
string tempFileSavePath, tempFileUrlPath;
TempFileHelper.CrateTempFilePath(".pdf", out tempFileSavePath, out tempFileUrlPath);
prsPres.SaveAs(tempFileSavePath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);

ViewWordHtml = string.Format(@"<iframe src=""{0}"" style=""width: 100%;Height:100%""></iframe>"
, Nav.AddHost(tempFileUrlPath));
}
catch (Exception ex)
{
ViewWordHtml = "请下载查看文件是否有破损";
}
}
public void ViewPdfAsHtmlByPath(string docAbsPath)
{
string tempFileSavePath, tempFileUrlPath;
tempFileSavePath = docAbsPath.Replace("\\", "/");
while (tempFileSavePath.Contains("//"))
tempFileSavePath = tempFileSavePath.Replace("//", "/");
string ss = HttpContext.Current.Server.MapPath("~");
ss = ss.Replace("\\", "/");
while (ss.Contains("//"))
ss = ss.Replace("//", "/");
string last = ss.Substring(ss.LastIndexOf("/"));

IList<string> paths = tempFileSavePath.SplitString(ss);
tempFileUrlPath = last + paths[0];
ViewWordHtml = string.Format(@"<iframe src=""{0}"" style=""width: 100%;Height:100%""></iframe>"
, Nav.AddHost(tempFileUrlPath));
}
public void ViewPictureAsHtmlByPath(string docAbsPath)
{
string tempFileSavePath, tempFileUrlPath;

tempFileSavePath = docAbsPath.Replace("\\", "/");
while (tempFileSavePath.Contains("//"))
tempFileSavePath = tempFileSavePath.Replace("//", "/");

string ss = HttpContext.Current.Server.MapPath("~");
ss = ss.Replace("\\", "/");
while (ss.Contains("//"))
ss = ss.Replace("//", "/");
string last = ss.Substring(ss.LastIndexOf("/"));

IList<string> paths = tempFileSavePath.SplitString(ss);
tempFileUrlPath = last + paths[0];
StringBuilder sb = new StringBuilder();
sb.Append(@"<div style=""text-align: center;"">");
sb.Append(string.Format(@"<img src=""{0}"" style=""width: 100%;Height:100% ;border: 1px solid black;""><br />",
Nav.AddHost(tempFileUrlPath)));
sb.Append("</div>");
ViewWordHtml = sb.ToString();
}
public void ViewTextAsHtmlByPath(string docAbsPath)
{
string tempFileSavePath, tempFileUrlPath;
tempFileSavePath = docAbsPath.Replace("\\", "/");
while (tempFileSavePath.Contains("//"))
tempFileSavePath = tempFileSavePath.Replace("//", "/");
string ss = HttpContext.Current.Server.MapPath("~");
ss = ss.Replace("\\", "/");
while (ss.Contains("//"))
ss = ss.Replace("//", "/");
string last = ss.Substring(ss.LastIndexOf("/"));


IList<string> paths = tempFileSavePath.SplitString(ss);
tempFileUrlPath = last + paths[0];
ViewWordHtml = string.Format(@"<iframe src=""{0}"" style=""width: 100%;Height:100%""></iframe>"
, Nav.AddHost(tempFileUrlPath));
}

猜你喜欢

转载自www.cnblogs.com/suqq/p/11670314.html
今日推荐