求助~!如何在.net中带图片导出EXCEL~

xinlongyue 2011-03-04 04:52:26
如果不带图片导出的话,就很简单。但如果要加上图片一起导出我只能做到固定列导出。
以下是我做的一个固定列导出的方法。
帮忙想想改怎么改改不用固定列导出啊~~~~~~
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);
...全文
427 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
李文辉 2011-10-31
  • 打赏
  • 举报
回复
看下这个吧,蛮详细的。
http://apps.hi.baidu.com/share/detail/21678670

淘宝网女装
按摩器十大品牌
海南网址导航
海南搜人才
zhu_wei1985 2011-03-05
  • 打赏
  • 举报
回复
LZ的代码我已经测试过了,主要是导出时必须是i<26,也就是只有26列吧。
for (int i = 0; i < 26; i++)
for (int c = 0; c < 26; c++)
我改成dt.Columns。count。试试~
wuyq11 2011-03-04
  • 打赏
  • 举报
回复
public void InsertPicture( string RangeName, string PicturePath, float PictuteWidth, float PictureHeight )
{
m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
m_objRange.Select();
float PicLeft, PicTop;
PicLeft = Convert.ToSingle(m_objRange.Left);
PicTop = Convert.ToSingle(m_objRange.Top);
m_objSheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight);
}


sheet.Shapes.AddPicture("C:""a.gif", Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, 250, 0, 70, 30);
Airch 2011-03-04
  • 打赏
  • 举报
回复
能带出来啊,我都能导出来啊,是不是你图片路径错啦,没找到图片
xinlongyue 2011-03-04
  • 打赏
  • 举报
回复
NPOI试过了。。但是不知道什么导出时,图片带不出来~
Airch 2011-03-04
  • 打赏
  • 举报
回复
用NPOI吧,导出图片非常简单,代码也不繁琐

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧