以下是sample code(只貼主要片段):
public void ExportToExcel(Excel excel,string tempfile,string stempid,ref int icount,ref int jcount)
{
Worksheet sheet=getTemplateSheet(tempfile,stempid);
Worksheet currentsheet=null;
string fname=sheet.Name;
int index=0;
if (icount>5||icount==1)
{
index=excel.Worksheets.Add();
excel.Worksheets[index].Copy(sheet);
currentsheet=excel.Worksheets[index];
// currentsheet.Pictures.Clear();
if (icount==1&&jcount==1)
{
excel.Worksheets[index].Name=fname+"(1)";
FillSheet(currentsheet,icount);
icount++;
}
if (icount>5)
{
jcount++;
excel.Worksheets[index].Name=fname+"("+jcount.ToString()+")";
icount=1;
FillSheet(currentsheet,icount);
icount++;
}
}
else
{
currentsheet=excel.Worksheets[fname+"("+jcount.ToString()+")"];
this.FillSheet(currentsheet,icount);
icount++;
}
for(int a=0;a<excel.Worksheets.Count;a++)
{
if (excel.Worksheets.Name=="Evaluation Copyright Warning"||excel.Worksheets.Name.Substring(0,5)=="Sheet")
excel.Worksheets.RemoveAt(a);
}
}
-----------------------------
public void FillSheet(Worksheet currentsheet,int icount)
{
Cells cells=currentsheet.Cells;
if (icount==1)
{
cells[0,1].PutValue(phasename);
cells[1,13].PutValue(model);
}
string image1,image2,image3;
image1=getPicLocation(partkey,int.Parse(optempid),1); //表示厚度的圖片所在路徑
image2=getPicLocation(partkey,int.Parse(optempid),2); //表示寬度的圖片所在路徑
image3=getPicLocation(partkey,int.Parse(optempid),4); //表示此部位的圖片所在路徑
switch (icount)
{
case 1:
cells["B4"].PutValue(PartNameCh+"("+PartNameEn+")");
break;
case 2:
cells["U4"].PutValue(PartNameCh+"("+PartNameEn+")");
break;
case 3:
cells["B23"].PutValue(PartNameCh+"("+PartNameEn+")");
break;
case 4:
cells["U23"].PutValue(PartNameCh+"("+PartNameEn+")");
break;
case 5:
cells["AN14"].PutValue(PartNameCh+"("+PartNameEn+")");
break;
}
//放置表示部位的圖片
if (image3!="")
{
if (File.Exists(image3))
{
//currentsheet.Pictures.Add(6,4,image3);
switch (icount)
{
case 1:
//currentsheet.Pictures.Add(6,4,12,12,image3);
currentsheet.Cells.ClearContents(4,1,21,19);
currentsheet.Pictures.Add(6,4,image3,100,100);
break;
case 2:
//currentsheet.Pictures.Add(6,23,12,31,image3);
currentsheet.Cells.ClearContents(4,20,21,38);
currentsheet.Pictures.Add(6,23,image3,100,100);
break;
case 3:
//currentsheet.Pictures.Add(25,4,31,12,image3);
currentsheet.Cells.ClearContents(23,1,40,19);
currentsheet.Pictures.Add(25,4,image3,100,100);
break;
case 4:
//currentsheet.Pictures.Add(25,23,31,31,image3);
currentsheet.Cells.ClearContents(23,20,40,31);
currentsheet.Pictures.Add(25,23,image3,100,100);
break;
case 5:
//currentsheet.Pictures.Add(16,42,22,50,image3);
currentsheet.Cells.ClearContents(14,39,40,57);
currentsheet.Pictures.Add(16,42,image3,100,100);
break;
}
}
}
}
以上兩個function是匯出XLS和往XLS中填充wmf圖片的主要片斷,它們會在一個循環中被調用,getPicLocation是取得圖片在Server上的路徑的function,用ref icount和ref jcount兩個變量分別用來控制每個worksheet上放置圖片的個數(這里是5個)和Worksheet的產生和命名。另外,附用到的wmf文件如下。謝謝!