日期:2014-05-17 浏览次数:21294 次
private string exclePath = @"E:\111.xls";
private int StartRow = 2; //读的起始行
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel = new Excel.Application();//引用Excel对象
Excel.Workbook 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.
{
Excel.Worksheet sheet = workbook.Worksheets.get_Item(i + 1) as Excel.Worksheet;//从1开始.
for (int row = StartRow; row <= sheet.UsedRange.Rows.Count; row++)
{
//取单元格值;
for (int col = 1; col <= sheet.UsedRange.Columns.Count; col++)
{
Excel.Range range =sheet.Cells[row, col] as Excel.Range;
sb.Append("," + col.ToString() + ":" + range.Text);
}
sb.Append(System.Environment.NewLine);
//取存图片;
if(sheet.Shapes.Count > row - StartRow )
{
Excel.Shape s = sheet.Shapes.Item(row - StartRow + 1) as Excel.Shape;
s.CopyPicture(Appearance.Button, Excel.XlCopyPictureFormat.xlBitmap); //COPY到内存。
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Bitmap))
{
pictureBox1.Image = (Bitmap)iData.GetData(DataFormats.Bitmap); //从内存取值;
pictureBox1.Image.Save(string.Format(@"D:\{0}.jpg", row)); //保存。
}
else
{
pictureBox1.Image = null;
}
}
}
}
workbook.Close(false,null,null);
excel.Quit();
}