About use aspose.excel add some pictures to worksheet

本版版主:

您好!因為工作關系我正在使用Aspose.Excel這個控件(評估版,正式版本正在購買中)開發相關項目。

在使用過程中遇到一個問題,問題如下:我需要在.XLS中的某些WorkSheet中放置一些.wmf格式的圖片(圖片放在別的Server上),每個Worksheet上放五張,超過五張則產生新的Worksheet來存放,依此類推。

但發現產生出來的Worksheet中的圖片只有第一個Worksheet中的一,二張圖片是正確的,其余的都和第二張重復,但經我跟蹤測試,程序運行時在代碼中得到的每張圖的路徑都是正確的。能不能請您幫我分析一下是什么原因呢?附產生的檔案如下:

能否将你的所有用到的wmf文件和sample code压缩一下贴在这里?这样我好尽快查出问题是什么,在哪里?谢谢。

以下是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.WorksheetsAngel [A].Name=="Evaluation Copyright Warning"||excel.WorksheetsAngel [A].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文件如下。謝謝!

Hi Joseph,

我测试了一下,没有发现问题。你用的是哪个版本?是不是最近的v3.5.1?

如果v3.5.1解决不了你的问题,能否将你的project(包括template file)整个压缩一下贴在这里?也可以发到nanjing@aspose.com .

謝謝回復。我也有寫過一段測試代碼,在我自己本機上測試(把圖片放在本機上),也是沒有發現問題.把整個project放上來的話,數據庫部分感覺有些不太好處理.

以下是我調用上面代碼的地方,不知對你分析是否有用:


public void OutputToExcel(string[][] arr1)
{
if (System.IO.File.Exists(resultfile))
System.IO.File.Delete(resultfile);

ClsReportBase.connServer(); //連結至圖片服務器
for(int k=0;k<arr1.Length;k++)
{
this.partkey=arr1[k][2];
CreateReport(arr1[k][0],arr1[k][1],arr1[k][2]); //optempid,opid,partkey
this.optempid=arr1[k][0];

}

}

public void CreateReport(string stempid,string sopid,string spartkey)
{
IReportService iReport=null;
Excel excel=ClsReportBase.getExcelInstance();
switch (stempid)
{
case "9":
iReport=new ClsUpperWelding(model,phase,sopid,stempid,spartkey,phaseName,tempfile,resultfile);
break;
case "10":
goto case "9";
case "2":
iReport=new ClsSkiving(model,phase,sopid,stempid,spartkey,phaseName,tempfile,resultfile);
break;
case "14":
iReport=new ClsSpliting(model,phase,sopid,stempid,spartkey,phaseName,tempfile,resultfile);
break;
case "15":
iReport=new ClsTapering(model,phase,sopid,stempid,spartkey,phaseName,tempfile,resultfile);
break;
default:
return;

}


if (!File.Exists(resultfile))
{
iReport.ExportToExcel(excel,this.tempfile,stempid,ref icount,ref jcount);
}
else
{
excel.Open(resultfile);
if (stempid!=this.optempid)
{
icount=1;
jcount=1;
}
if (stempid=="9"||stempid=="10")
{
iReport.ExportToExcel(excel,tempfile,stempid,ref icount,ref jcount);

}
if (stempid=="2")
{
iReport.ExportToExcel(excel,tempfile,stempid,ref icount,ref jcount);
}
if (stempid=="14")
{
iReport.ExportToExcel(excel,tempfile,stempid,ref icount,ref jcount);
}
if (stempid=="15")
{
iReport.ExportToExcel(excel,tempfile,stempid,ref icount,ref jcount);
}
}
excel.Save(resultfile,FileFormatType.Default);

}

你看看還有哪段需要我貼上來的話請告訴我。謝謝!

谢谢你贴了那么多的代码。不过目前还看不出是什么引起的问题。能否请你:

1。如果可能的话,写一个简单的case重现这个问题。这个问题可能和template file,图片,Worksheet.Copy, Worksheet.RemoveAt 方法有关系。所以请删除你所有和数据库相关的代码,所有设置数据和相关样式的代码。只保留和我说的几个方面有关的代码。看看能否重现这个问题。

2。如果不行的话,请你把整个project zip以后贴上来,我来梳理一下代码,看哪里会导致这个问题。

非常感谢。

謝謝您的回復.我想我還是把整個project放上來吧。說明一下:

這部分code是我們project(基于web service)中的報表部分,所以分為Web Service和Client端兩部分.(分別在PDMBOMWebService.rar和ReportClient.rar文件中),數據庫存取部分的功能都封裝在一個名為MBOMProj.dll的文件中,并在Web Service中加以引用.相信你一看便 知,所以為節省時間這些地方我就不刪除了。如像:return ClsGlobal.objDataConnect.DataQuery(strSql);這樣的code就是了。

還請您 幫我檢查一下看到底是哪里的毛病.如還有什么需要也請告訴我。謝謝!

Laurence:

您好,我還想問個問題:為什么用評估版本用Worksheets.add()和Worksheets.copy(sheet)產生出來的Worksheet上會產生很多的亂字符呢?

是版本的原因嗎?

关于乱字符,是由于用的评估版的原因。请查看Licensing 。我将尽快检查图片相关的问题。

顺便问一下,所有的图片是放在数据库里,还是放在本地磁盘上?

圖片是放在遠端Server上,數據庫中只存放圖片的路徑信息:目錄,文件名(有進行加密處理).謝謝!

Laurence,你好!請 問 我上星期提出的問題有沒有幫我檢查出是什么原因呢?

可能是由于使用Worksheet.Copy方法从你的template file拷贝图片到另一个文件引起的。我正在检查和修改。可以使用以下代码发现相关的问题:

Excel excel = new Excel();
excel.Open("d:\\PFC _FORM_Template.XLS");
Excel outExcel = new Excel();

outExcel.Worksheets[0].Copy(excel.Worksheets["Skiving Instructions"]);

outExcel.Save("d:\\book1.xls");

Hi Joseph,

I fixed this bug. Please try this attached fix.

謝謝您的回復,我用修復后的dll測試過了,它只解決了之前的另一個問題:把一個xls文件中的模板worksheet上的圖片copy到另一個xls文件中的worksheet時出現亂碼的問題。但是,圖片重復出現的問題還是沒有解決。需要說明的是,我這里得到圖片文件路徑的方式是:把遠端的Server用net use命令將其映射為一個本地驅動器盤符,然后從數據庫取得文件名等信息組合成文件的路徑.請問是否是這樣做引起的問題呢?

能否试试将图片拷贝到本地,然后进行处理?或者将图片放在stream中,然后插入文件?

Hi Joseph,

I found the root of this problem finally. Please try this attached fix.

Dear Laurence:

謝謝您的回復,但很遺憾,我用這個最新版本(3.5.1.4),并采用將文件copy到本地機器,插入時把文件轉換成Stream格式,還是會出現重復.

Dear Laurence:

還有一個怪問題:若我連續操作(產生xls文件),發現每次產生的Worksheet上的圖片會出現丟失的現象。