C# 在web应用程序中从excel中提取图像?
当我将excel文件上载到服务器时,我希望提取上载文件中的图像并将其存储在根目录中。我在windows应用程序中尝试过它,它运行良好,但似乎不适用于web应用程序C# 在web应用程序中从excel中提取图像?,c#,.net,excel,web-applications,C#,.net,Excel,Web Applications,当我将excel文件上载到服务器时,我希望提取上载文件中的图像并将其存储在根目录中。我在windows应用程序中尝试过它,它运行良好,但似乎不适用于web应用程序 Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(file,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1]; //Selects the first sheet
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, 1]; //Select cell A1
object cellValue = range.Value2;
#region Extract the image
Microsoft.Office.Interop.Excel.Picture pic = (Microsoft.Office.Interop.Excel.Picture)ws.Pictures(1);
if (pic != null)
{
//This code will detect what the region span of the image was
int startCol = (int)pic.TopLeftCell.Column;
int startRow = (int)pic.TopLeftCell.Row;
int endCol = (int)pic.BottomRightCell.Column;
int endRow = (int)pic.BottomRightCell.Row;
pic.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
//From here How to save in the root directory ??
}
#endregion
//Close the workbook
wb.Close(false, Type.Missing, Type.Missing);
//Exit Excel
excelApp.Quit();
这段代码包含检索图像,但我不知道如何将其保存在根文件夹中
我对从电子表格中提取图像的其他方法也持开放态度。以下代码适用于web和桌面应用程序,将电子表格中的图像提取到根目录中的文件夹中
//'file' could be a HttpPostedFileBase from client side
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("~/UploadedPO"), fileName);
file.SaveAs(physicalPath);
Excel.Application excelApp = new Excel.Application();
Excel.Workbook wb = excelApp.Workbooks.Open(physicalPath,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);
Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
var app = new Excel.Application();
Excel.Workbook workbook = wb;
Excel.Worksheet wsCurrent = ws;
Excel.XlFileFormat format = Excel.XlFileFormat.xlHtml;
wsCurrent.SaveAs(physicalPath, format);
workbook.Close();