能直接使用的函数
public void CopyExcel(string path)
{
string exclePath = path;
int StartRow = 1; //读的起始行
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//引用Excel对象
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(exclePath);
//dynamic excel = new Microsoft.Office.Interop.Excel.Application();//引用Excel对象
//dynamic workbook = excel.Workbooks.Add(exclePath);
excel.UserControl = true;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
excel.Visible = false;
for (int i = 0; i < workbook.Worksheets.Count; i++)//循环取所有的Sheet.
{
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Worksheets.get_Item(i + 1) as Microsoft.Office.Interop.Excel.Worksheet;//从1开始.
// dynamic sheet = workbook.Worksheets.get_Item(i + 1) as Microsoft.Office.Interop.Excel.Worksheet;//从1开始.
for (int row = StartRow; row <= sheet.UsedRange.Rows.Count; row++)
{
//取单元格值;
for (int col = 1; col <= sheet.UsedRange.Columns.Count; col++)
{
Microsoft.Office.Interop.Excel.Range range = sheet.Cells[row, col] as Microsoft.Office.Interop.Excel.Range;
// dynamic range = sheet.Cells[row, col] as Microsoft.Office.Interop.Excel.Range;
sb.Append("," + col.ToString() + ":" + range.Text);
}
sb.Append(System.Environment.NewLine);
//取存图片;
if (sheet.Shapes.Count > row - StartRow)
{
Microsoft.Office.Interop.Excel.Shape s = sheet.Shapes.Item(row - StartRow + 1) as Microsoft.Office.Interop.Excel.Shape;
// dynamic s = sheet.Shapes.Item(row - StartRow + 1) as Microsoft.Office.Interop.Excel.Shape;
Clipboard.Clear();//Clipboard类是引用 .NET组件中System.Windows.Forms的
s.CopyPicture(Appearance.Button, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap); //COPY到内存。
IDataObject iData = Clipboard.GetDataObject();
if (iData != null && iData.GetDataPresent(DataFormats.Bitmap))
{
System.Drawing.Image img = Clipboard.GetImage(); //从内存读取图片
if (img != null)
{
//保存图片位置
img.Save(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "另存为.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //保存到本地
}
}
else
{
}
}
}
}
workbook.Close(false, null, null);
excel.Quit();
}