求助~!如何在.net中带图片导出EXCEL~
如果不带图片导出的话,就很简单。但如果要加上图片一起导出我只能做到固定列导出。
以下是我做的一个固定列导出的方法。
帮忙想想改怎么改改不用固定列导出啊~~~~~~
protected bool ExportToExcel(System.Data.DataTable dt, string AbosultedFilePath, string fileName, float cWidth, float cHeight)
{
if (dt == null) return false;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
System.Web.HttpContext.Current.Response.Write("无法创建Excel对象,可能你的电脑未装Excel");
return false;
}
if (!Directory.Exists(AbosultedFilePath)) Directory.CreateDirectory(AbosultedFilePath);
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1
Microsoft.Office.Interop.Excel.Range range = null;
//worksheet.Shapes.AddPicture(AbosultedFilePath + fileName + ".jpg", MsoTriState.msoFalse, MsoTriState.msoTrue, 2, 2, cWidth, cHeight);
int rowIndex = 2;// Convert.ToInt32(cHeight / 13.5) + 2;
//栏宽
((Range)worksheet.Columns["A", Type.Missing]).ColumnWidth = 20;
((Range)worksheet.Columns["B", Type.Missing]).ColumnWidth = 11;
((Range)worksheet.Columns["C", Type.Missing]).ColumnWidth = 11;
((Range)worksheet.Columns["D", Type.Missing]).ColumnWidth = 20;
((Range)worksheet.Columns["E", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["F", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["G", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["H", Type.Missing]).ColumnWidth = 11;
((Range)worksheet.Columns["I", Type.Missing]).ColumnWidth = 8;
((Range)worksheet.Columns["J", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["K", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["L", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["M", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["N", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["O", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["P", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["Q", Type.Missing]).ColumnWidth = 13;
((Range)worksheet.Columns["R", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["S", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["T", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["U", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["V", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["W", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["X", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["Y", Type.Missing]).ColumnWidth = 10;
((Range)worksheet.Columns["Z", Type.Missing]).ColumnWidth = 10;
//((Range)worksheet.Columns["A", "Z"]).AutoFit();
//列高,前2行一定要設,和圖片相互印
((Range)worksheet.Rows[1, Type.Missing]).RowHeight = 20;
((Range)worksheet.Rows[2, Type.Missing]).RowHeight = 20;
for (int i = 0; i < 26; i++)
{
worksheet.Cells[rowIndex, i + 2] = dt.Columns[i].ToString();
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[rowIndex, i + 2];
range.Font.Bold = true; //粗体
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
range.WrapText = false;//换行
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //背景色
//range.EntireColumn.AutoFit(); //自动设置列宽
//range.EntireRow.AutoFit(); //自动设置行高
}
string picName;
float picTop = 41;
for (int r = 0; r < dt.Rows.Count; r++)
{
picName = "d:\\photo\\" + (dt.Rows[r]["item_iid"].ToString()) + ".jpg";
// worksheet.Cells[r + rowIndex + 1, 1] = picName;
if (File.Exists(picName)) worksheet.Shapes.AddPicture(picName, MsoTriState.msoTrue, MsoTriState.msoTrue, 2, picTop, cWidth, cHeight);
for (int c = 0; c < 26; c++)
{
worksheet.Cells[r + rowIndex + 1, c + 2] = dt.Rows[r][c].ToString();
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + rowIndex + 1, c + 2];
range.WrapText = true;//换行
range.Font.Size = 10; //字体大小
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); //加边框
//range.EntireColumn.AutoFit(); //自动调整列宽
range.RowHeight = cHeight + 4;
}
picTop += cHeight + (float)3.52;
System.Windows.Forms.Application.DoEvents();
}
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
if (dt.Columns.Count > 1)
{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
}
try
{
workbook.Saved = true;
workbook.SaveCopyAs(AbosultedFilePath + fileName + ".xls");
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("导出文件时出错,文件可能正被打开!\n" + ex.ToString());
return false;
}
workbooks.Close();
if (xlApp != null)
{
xlApp.Workbooks.Close();
xlApp.Quit();
int generation = System.GC.GetGeneration(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
System.GC.Collect(generation);
}
GC.Collect(); //强行销毁
#region //强行杀死最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killID = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc[m].StartTime)
{
startTime = excelProc[m].StartTime;
killID = m;
}
}
if (excelProc[killID].HasExited == false)
{
excelProc[killID].Kill();
}
#endregion
//---将保存的Excel下载到本地---//
saveExcel(AbosultedFilePath + fileName + ".xls");
{
return true;
}
}
public static void saveExcel(string FileName)
{
try
{
string FullFileName = FileName;
//FileName--要下载的文件名
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.Clear();
curContext.Response.ClearHeaders();
curContext.Response.Buffer = false;
curContext.Response.ContentType = "application/octet-stream";
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
curContext.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
curContext.Response.WriteFile(DownloadFile.FullName);
curContext.Response.Flush();
DownloadFile.Delete();
curContext.Response.End();
}
else
{
}
}
catch
{
}
}
protected void btnToExcel_Click(object sender, EventArgs e)
{
if (Session["GV"] == null)
Response.Write("<script language='javascript'>alert('請先查詢再轉出Excel')</script>");
else
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString = links.conStr;
OdbcCommand comm = new OdbcCommand();
comm.Connection = conn;
OdbcDataAdapter ada = new OdbcDataAdapter();
ada.SelectCommand = comm;
DataSet DS = new DataSet();
comm.CommandText = getSQL();
conn.Open();
ada.Fill(DS);