各位大俠下午好:
小弟在把多個由水晶報表導出的excel文件合并到一個excel文件的時候發現有一下問題:
圖片和表格重疊在一起,也就是說報表的圖形遮住了表格文字。如果我分別創建幾個excel文件,每個文件里都有圖片和文字。用同樣的方法(代碼如下)卻沒有問題(圖片和文字是分開的)。請問各位大哥該如何解決這一問題。多謝!!!
private void MergeExcelFiles(string fileName, System.Collections.ArrayList
files)
{
Excel.Application exlApp = new Excel.ApplicationClass();
exlApp.Application.SheetsInNewWorkbook = 1;
Excel.Workbook excelWorkBook =
exlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
excelWorkBook = exlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet excelWorkSheet =
(Excel.Worksheet)excelWorkBook.ActiveSheet;
excelWorkBook.Saved = true;
excelWorkBook.SaveCopyAs(fileName);
excelWorkBook.Close(null, null, null);
excelWorkBook = null;
excelWorkSheet = null;
exlApp.Application.Visible = false;
exlApp.Application.AlertBeforeOverwriting = false;
object missing = System.Reflection.Missing.Value;
Excel.Workbook wkBookDest;
Excel.Workbook wkBookOrign;
try
{
wkBookDest = exlApp.Workbooks.Open(fileName,
missing, false, missing, missing, missing,
missing,missing, missing,true, missing,
missing, missing, missing, missing);
for(int i=0; i< files.Count; i++)
{
string OrignfileName = files[i].ToString();
wkBookOrign = exlApp.Workbooks.Open(OrignfileName,
missing, true, missing, missing, missing,
missing, missing, missing, false, missing,
missing, missing, missing, missing);
int nOrignSheetsCount = wkBookOrign.Worksheets.Count;
int nDestSheetsCount = wkBookDest.Worksheets.Count;
Excel.Worksheet wkSheetDest;
wkSheetDest = wkBookDest.Worksheets.Add(missing,
wkBookDest.Worksheets[nDestSheetsCount],
nOrignSheetsCount,
Excel.XlSheetType.xlWorksheet) as Excel.Worksheet;
for(int j=1; j<= nOrignSheetsCount; j++)
{
Excel.Worksheet wkSheetOrign = wkBookOrign.Worksheets[j] as
Excel.Worksheet;
wkSheetDest = wkBookDest.Worksheets[nDestSheetsCount + j] as
Excel.Worksheet;
wkSheetDest.Activate();
wkSheetDest.Name = GetSheetName(OrignfileName);
Excel.Range range =
wkSheetOrign.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, missing);
int nRow = range.Row;
int nColumn = range.Column;
Excel.Range rngOrign =
wkSheetOrign.get_Range(wkSheetOrign.Cells[1,1],wkSheetOrign.Cells[nRow,
nColumn]);
Excel.Range rngDest = wkSheetDest.get_Range(wkSheetDest.Cells[1,1],
wkSheetDest.Cells[nRow, nColumn]);
rngOrign.Copy(missing);
wkSheetDest.Paste(rngDest, missing);
wkBookDest.Save();
}
wkBookOrign.Close(false, null, null);
}
Excel.Worksheet temp = (Excel.Worksheet)(wkBookDest.Worksheets[1]);
temp.Delete();
temp = null;
wkBookDest.Save();
wkBookDest.Close(null, null, null);
}
catch(Exception ex)
{
CommonModule.Functions.ShowErrorMessageBox(ex.Message, "保存錯誤");
}
finally
{
wkBookDest = null;
wkBookOrign = null;
exlApp.Quit();
exlApp = null;
GC.Collect();
}
CommonModule.Functions.DeleteTempFiles();
}
private string GetSheetName(string fullName)
{
string fileName = new System.IO.FileInfo(fullName).Name;
int index = fileName.IndexOf(".");
string sheetName = fileName.Substring(0, index);
return sheetName;
}