private void Invoke()
{
ArrayList arrlist = new ArrayList();
DataTable tab1 = UtlBiz.GetDefaultTransaction().GetTable( "select * from Customers" );
DataTable tab2 = UtlBiz.GetDefaultTransaction().GetTable( "select * from Employees" );
arrlist.Add( tab1 );
arrlist.Add( tab2 );
string webFileTmp = "Files\\aaa.xls";//模板路徑
string webFileTar = "Files\\ttt.xls";//目標路徑
string phyFileTmp = Server.MapPath( webFileTmp );//模板路徑
string phyFileTar = Server.MapPath( webFileTar );//目標路徑
ExcDataOnTmpA( arrlist, phyFileTmp, phyFileTar );
}
//============================================================
//導出文件(需要源文件名和模板文件名)
//============================================================
private void ExcDataOnTmpA( ArrayList arrlist, string tmpFileName, string phyFileTar )
{
// Copy File to tarFileName
CopyFile( tmpFileName, phyFileTar );
ArrayList substItemList = GetSubstCellList( phyFileTar, 1, 100, 100 );
SubstituteCellData( arrlist, substItemList, phyFileTar );
}
//============================================================
//複製模板文件
//============================================================
private bool CopyFile( string phyFileTmp, string phyFileTar )
{
if( !File.Exists( phyFileTmp ))
return false;
try{
File.Copy( phyFileTmp, phyFileTar, true );
return true;
}catch( Exception ex ){
return false;
}
}
//============================================================
//填充單元格並保存目標文件
//============================================================
private void SubstituteCellData( ArrayList arrList, ArrayList subsItemList, string phyFileTar )
{
Excel.Application excelApp = new Excel.ApplicationClass(); //創建一個Application對象
Excel.Workbook workBook = excelApp.Workbooks._Open( phyFileTar, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value );
Excel.Worksheet workSheet = ( Excel.Worksheet )workBook.ActiveSheet;
try{
//遍歷單元格
CopyExcelFormat( workSheet, arrList, subsItemList );
foreach ( SubstCell substCell in subsItemList ) FillSubstCellData( workSheet, arrList, substCell );//填充單元格
workBook.Save();
}catch( Exception Er ){
throw new Exception( "調用EXCEL程序出現錯誤!" + Er.Message );
}finally{
if( workBook != null ) workBook.Close( false, Missing.Value, Missing.Value );
if( excelApp != null ) excelApp.Quit();
}
}
//===============================================================
//驗證格式是否爲0:MAmount:F並判斷是否是多行記錄並填充數據。T代表的是多行
//===============================================================
private void FillSubstCellData( Excel.Worksheet workSheet, ArrayList dataList, SubstCell substCellItem )
{
string cellStr = substCellItem.cellStr;//定義的單元格內容
ArrayList strList = UtlMisc.GetItemsListFromString( cellStr, ":" );//單元格內容用":"隔開
int dataIndex = int.Parse(strList[0].ToString());//取數據集中第幾張表
string fieldName = strList[1].ToString();//取表中的字段名
bool isList = strList[2].ToString().ToUpper() == "T";//判斷是否是多行記錄
if( isList ){//如果是多行記錄
DataTable tbl = dataList[ dataIndex ] as DataTable;//把符合條件的轉換成datatable類型
//複製單元格格式
for ( int i = 0; i < tbl.Rows.Count - 1; i++ ){
workSheet.Cells[ substCellItem.rowIndex + i, substCellItem.coluIndex ] = tbl.Rows[i][fieldName]; }
}else{
workSheet.Cells[ substCellItem.rowIndex, substCellItem.coluIndex ] = ((DataTable)dataList[dataIndex]).Rows[0][fieldName];
}
}
//=============================================================
//複製格式
//=============================================================
private void CopyExcelFormat( Excel.Worksheet workSheet, ArrayList dataList, ArrayList subsItemList )
{
int copyRowCount = 0;
foreach( SubstCell substCell in subsItemList ){
string cellStr = substCell.cellStr;//定義的單元格內容
ArrayList strList = UtlMisc.GetItemsListFromString( cellStr, ":" );//單元格內容用":"隔開
int dataIndex = int.Parse( strList[0].ToString() );//取數據集中第幾張表
string fieldName = strList[1].ToString();//取表中的字段名
bool isList = strList[2].ToString().ToUpper() == "T";//判斷是否是多行記錄
if( isList ){//如果是多行記錄
DataTable tbl = dataList[dataIndex] as DataTable;
int rowsCount = tbl.Rows.Count;
if ( rowsCount > copyRowCount ) copyRowCount = rowsCount;
}
}
for( int i = 1; i < copyRowCount - 1; i++ ){
(( Excel.Range )workSheet.Rows[7, Type.Missing] ).Copy( Type.Missing );
(( Excel.Range )workSheet.Cells[7 + i, 1] ).EntireRow.Insert( 0, 0 );
for( int j = 1; j < workSheet.UsedRange.Columns.Count +3 ; j++ ) workSheet.Cells[7 + i, j] = "";
}
}
//===========================================================
//模板的行、列和單元格
//===========================================================
public struct SubstCell
{
public int rowIndex;//模板的行
public int coluIndex;//模板的列
public string cellStr;//模板的單元格數據
}
//============================================================
//判斷格式0:MAmount:F
//============================================================
private bool IsDataFieldCell( string cellStr )
{
if( string.IsNullOrEmpty( cellStr )) return false;//判斷是否爲空或""
ArrayList list = UtlMisc.GetItemsListFromString( cellStr, ":" );//截取字符串並用":"隔開
if( list.Count != 3 ) return false;//判斷字符串的個數是否爲3
if( !UtlMisc.IsIntString( list[0].ToString()) ) return false;//判斷第一個字符是否是數字
return true;
}
//===========================================================
//讀取Excel,取出單元格
//===========================================================
private ArrayList GetSubstCellList( string phyFileTar, int sheetIndex, int rowCount, int columnCount )
{
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
//打開模板文件,得到WorkBook對象
Excel.Workbooks workBooks = excelApp.Workbooks;
Excel.Workbook workBook = workBooks._Open( phyFileTar, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value );
ArrayList list = new ArrayList();
try{
//得到worksheet對象
Excel.Worksheet workSheet = ( Excel.Worksheet )workBook.Sheets[sheetIndex];//找到所需要的sheet
if( workSheet == null ) return null;
if( rowCount == 0 ) rowCount = workSheet.UsedRange.Rows.Count;//設置已用單元格的行數
if( columnCount == 0 ) columnCount = workSheet.UsedRange.Columns.Count;//設置已用單元格的列數
for( int i = 1; i <= rowCount; ++i )
for( int j = 1; j <= columnCount; ++j ){
SubstCell cellItem = new SubstCell();//創建單元格對象
cellItem.rowIndex = i;
cellItem.coluIndex = j;
Excel.Range range = (Excel.Range)workSheet.Cells[i, j];//取出單元格
if( range.Value2 != null )
cellItem.cellStr = range.Value2.ToString();
if( IsDataFieldCell(cellItem.cellStr )) list.Add( cellItem );
}
}catch ( Exception Er ){
throw new Exception( "調用EXCEL程序出現錯誤!" + Er.Message );
}
finally{
if( workBook != null ) workBook.Close( false, Missing.Value, Missing.Value );
if( workBooks != null ) workBooks.Close();
if( excelApp != null ) excelApp.Quit();
}
return list;
}
}