热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c#中Excel数据的导入、导出

**导出到Excel文件含完整路径含字段标题
ExpandedBlockStart.gifContractedBlock.gif/**//// 
InBlock.gif
/// 导出到 Excel 文件
InBlock.gif
/// 

InBlock.gif
/// 含完整路径
ExpandedBlockEnd.gif
/// 含字段标题名

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
/**//// 
InBlock.gif
/// 从Excel导入帐户(逐单元格读取)
InBlock.gif
/// 

ExpandedBlockEnd.gif
/// 完整路径名

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 &#61; 2; i < 4; i&#43;&#43;)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for ( int j &#61; 1; j < 21; j&#43;&#43;)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Excel.Range range 
&#61; 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
/**//// 
InBlock.gif
/// 从Excel导入帐户(新建oleDb连接,Excel整表读取,适于无合并单元格时)
InBlock.gif
/// 

InBlock.gif
/// 完整路径名
ExpandedBlockEnd.gif
/// 

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

None.gif
None.gif
None.gifdataGrid中显示&#xff1a;
None.gifDataGrid1.DataMember
&#61; "[Sheet1$]" ;
None.gifDataGrid1.DataSource 
&#61; myDataSet ;

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


推荐阅读
author-avatar
c6643e7f36_253
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有