ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif
/// 导出到 Excel 文件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="fileName">含完整路径</param>
ExpandedBlockEnd.gif
/// <param name="dataTable">含字段标题名</param>

None.gifpublic void ExpExcel(string fileName ,DataTable dataTable)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    Excel.ApplicationClass apc 
=new Excel.ApplicationClass();
InBlock.gif
InBlock.gif    apc.Visible 
= false ;
InBlock.gif    Excel.Workbook wkbook 
= apc.Workbooks.Add( true ) ;
InBlock.gif    Excel.Worksheet wksheet 
= (Excel.Worksheet)wkbook.ActiveSheet;
InBlock.gif
InBlock.gif    
int rowIndex = 2;
InBlock.gif    
int colIndex = 1;
InBlock.gif
InBlock.gif    wksheet.get_Range(apc.Cells[
1,1],apc.Cells[dataTable.Rows.Count,dataTable.Columns.Count]).NumberFormat = "@";
InBlock.gif
InBlock.gif    
//取得列标题
InBlock.gif
    foreach (DataColumn dc in dataTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        colIndex 
++;
InBlock.gif        wksheet.Cells[
1,colIndex] = dc.ColumnName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//取得表格中数据
InBlock.gif
    foreach (DataRow dr in dataTable.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        colIndex 
= 1;
InBlock.gif        
foreach (DataColumn dc in dataTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(dc.DataType == System.Type.GetType("System.DateTime"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                apc.Cells[rowIndex,colIndex] 
= "'"+(Convert.ToDateTime(dr[dc.ColumnName].ToString())).ToString("yyyy-MM-dd");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
if(dc.DataType == System.Type.GetType("System.String"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                apc.Cells[rowIndex,colIndex] 
= "'"+dr[dc.ColumnName].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                apc.Cells[rowIndex,colIndex] 
= "'"+dr[dc.ColumnName].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            wksheet.get_Range(apc.Cells[rowIndex,colIndex],apc.Cells[rowIndex,colIndex]).HorizontalAlignment 
= Excel.XlHAlign.xlHAlignLeft;
InBlock.gif
InBlock.gif            colIndex
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        rowIndex
++;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
//设置表格样式
InBlock.gif
    wksheet.get_Range(apc.Cells[1,1],apc.Cells[1,dataTable.Columns.Count]).Interior.ColorIndex = 20; 
InBlock.gif    wksheet.get_Range(apc.Cells[
1,1],apc.Cells[1,dataTable.Columns.Count]).Font.ColorIndex = 3;
InBlock.gif    wksheet.get_Range(apc.Cells[
1,1],apc.Cells[1,dataTable.Columns.Count]).Borders.Weight = Excel.XlBorderWeight.xlThin;
InBlock.gif    wksheet.get_Range(apc.Cells[
1,1],apc.Cells[dataTable.Rows.Count,dataTable.Columns.Count]).Columns.AutoFit();
InBlock.gif
InBlock.gif    
if(File.Exists(fileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        File.Delete(fileName);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    wkbook.SaveAs( fileName ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
InBlock.gif   
InBlock.gif    wkbook.Close(Type.Missing,Type.Missing,Type.Missing);
InBlock.gif    apc.Quit();
InBlock.gif    wkbook 
= null;
InBlock.gif    apc 
= null;
InBlock.gif    GC.Collect();
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// 从Excel导入帐户(逐单元格读取)
InBlock.gif
/// </summary>
ExpandedBlockEnd.gif
/// <param name="fileName">完整路径名</param>

None.gifpublic IList ImpExcel(string fileName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    IList alExcel 
= new ArrayList();
InBlock.gif    UserInfo userInfo 
= new UserInfo();
InBlock.gif
InBlock.gif    Excel.Application app;
InBlock.gif    Excel.Workbooks wbs;
InBlock.gif    Excel.Worksheet ws;
InBlock.gif
InBlock.gif    app 
= new Excel.Application();
InBlock.gif    wbs 
= app.Workbooks;
InBlock.gif    wbs.Add(fileName);
InBlock.gif    ws
= (Excel.Worksheet)app.Worksheets.get_Item(1);
InBlock.gif    
int a = ws.Rows.Count;
InBlock.gif    
int b = ws.Columns.Count;
InBlock.gif    
InBlock.gif    
for ( int i = 2; i < 4; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for ( int j = 1; j < 21; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Excel.Range range 
= ws.get_Range(app.Cells[i,j],app.Cells[i,j]);
InBlock.gif            range.Select();
InBlock.gif            alExcel.Add( app.ActiveCell.Text.ToString() );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return alExcel;
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// 从Excel导入帐户(新建oleDb连接,Excel整表读取,适于无合并单元格时)
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="fileName">完整路径名</param>
ExpandedBlockEnd.gif
/// <returns></returns>

None.gifpublic DataTable ImpExcelDt (string fileName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + fileName + ";Extended Properties=Excel 8.0" ;
InBlock.gif    OleDbConnection myConn 
= new OleDbConnection ( strCon ) ;
InBlock.gif    
string strCom = " SELECT * FROM [Sheet1$] " ;
InBlock.gif    myConn.Open ( ) ;
InBlock.gif    OleDbDataAdapter myCommand 
= new OleDbDataAdapter ( strCom , myConn ) ;
InBlock.gif    DataSet myDataSet 
= new DataSet ( ) ;
InBlock.gif    myCommand.Fill ( myDataSet , 
"[Sheet1$]" ) ;
InBlock.gif    myConn.Close ( ) ;
InBlock.gif
InBlock.gif    DataTable dtUsers 
= myDataSet.Tables[0];
InBlock.gif
InBlock.gif    
return dtUsers;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifdataGrid中显示:
None.gifDataGrid1.DataMember
= "[Sheet1$]" ;
None.gifDataGrid1.DataSource 
= myDataSet ;

转载于:https://www.cnblogs.com/liuzhixian/articles/851983.html

更多推荐