asp.net 導出excel帶圖片

protected void btgua_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
if (dt != null)
{
#region 操做excel
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
xlWorkBook = new Microsoft.Office.Interop.Excel.Application().Workbooks.Add(Type.Missing);
xlWorkBook.Application.Visible = false;
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];dom

//設置標題字體

xlWorkSheet.Cells[1, 1] = "發送內容";
xlWorkSheet.Cells[1, 2] = "發送時間";
xlWorkSheet.Cells[1, 3] = "發送圖片";
//設置寬度
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 2]).ColumnWidth = 15;
//設置字體
xlWorkSheet.Cells.Font.Size = 12;
#region 爲excel賦值
for (int i = 0; i < dt.Rows.Count; i++)
{
//爲單元格賦值。
xlWorkSheet.Cells[i + 2, 1] = dt.Rows[i]["SendInfo"].ToString();
xlWorkSheet.Cells[i + 2, 2] = dt.Rows[i]["SendTime"].ToString();
#regionui

//能夠直接取圖片的地址
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Temp1"].ToString()))
{
string filename = Server.MapPath("upload/nianhuo/") + dt.Rows[i]["Temp1"].ToString();
#endregion
int rangeindex = i + 2;
string rangename = "C" + rangeindex;this

Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);

range.Select();
/////////////
float PicLeft, PicTop, PicWidth, PicHeight;    //距離左邊距離,頂部距離,圖片寬度、高度
PicTop = Convert.ToSingle(range.Top);
PicWidth = Convert.ToSingle(range.MergeArea.Width);
PicHeight = Convert.ToSingle(range.Height);
PicWidth = Convert.ToSingle(range.Width);
PicLeft = Convert.ToSingle(range.Left);
////////////////////excel

Microsoft.Office.Interop.Excel.Pictures pict = (Microsoft.Office.Interop.Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);
if (filename.IndexOf(".") > 0)
{
if (System.IO.File.Exists(filename))
{
// pict.Insert(filename, Type.Missing);//顯示原圖 重疊在一塊兒
xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, PicWidth, PicHeight);//指定位置顯示小圖
}
}
}
// ActiveWorkbookcode

 


}
#endregion
#region 保存excel文件
Random myrand= new Random();
string filenamess = System.DateTime.Now.ToString("yyyyMMddhhMMss") + myrand.Next(0, 100);
string filePath = Server.MapPath("excel")+@"\" +filenamess+".xls";
xlWorkBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkBook.Application.Quit();
xlWorkSheet = null;
xlWorkBook = null;
GC.Collect();
System.GC.WaitForPendingFinalizers();
#endregion
#endregion對象

#region 導出到客戶端
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filenamess, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "Application/excel";
Response.WriteFile(filePath);
Response.End();
#endregion
KillProcessexcel("EXCEL");進程

}圖片

}
#region 殺死進程
private void KillProcessexcel(string processName)
{ //得到進程對象,以用來操做
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//獲得全部打開的進程
try
{
//得到須要殺死的進程名
foreach (Process thisproc in Process.GetProcessesByName(processName))
{ //當即殺死進程
thisproc.Kill();
}
}
catch (Exception Exc)
{
throw new Exception("", Exc);
}
}
#endregionget

 

 

using System.Reflection;
using System.Diagnostics;

using Microsoft.Office.Interop.Excel;

Using Office;

相關文章
相關標籤/搜索