public object ImportDateCreate()
        {
            
// TODO:  添加 DJPlanExpImpFactory.Import 实现
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory 
= "C:\\";
            openFileDialog1.FileName    
= "未命名";
            openFileDialog1.Filter        
= "Excel 文件|*.xls";
            openFileDialog1.Title        
= "由Excel文件导入";
            System.Windows.Forms.DialogResult dialogResult 
= openFileDialog1.ShowDialog();
            
if(dialogResult != System.Windows.Forms.DialogResult.OK)
            {
                
return null;
            }    
            
string filePath = openFileDialog1.FileName;
            
if(filePath.Length == 0)
                
return null;
            
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
            
"Data Source="+ filePath +";"+"Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\""
            OleDbConnection conn 
= new OleDbConnection(strConn); 
            conn.Open(); 
            
string strExcel = string.Empty; 
            DataTable schemaTable 
= conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null); 
            
int TC = schemaTable.Rows.Count;
            
string[] tableName = new string[TC];
            
for(int m = 0 ;m < TC;m++)
            {
                
string temp = schemaTable.Rows[m][2].ToString().Trim();
                
//                temp = temp.Remove( temp.Length - , 1);
                tableName[m] = temp.Substring(0,(temp.Length));
            }
            OleDbDataAdapter myCommand 
= null
            DataSet ds 
= null;  
            
//只有一个Sheet
            string sheetName = tableName[0];
            strExcel
="select * from [" + sheetName + "]"
            myCommand 
= new OleDbDataAdapter(strExcel, strConn); 
            ds 
= new DataSet(); 
            DataSet result 
= new DataSet();
            myCommand.Fill(ds,
"table1"); 
            myCommand.Fill(result, 
0, ds.Tables[0].Rows.Count + 1"table1");
            conn.Close();
            ds 
= new DataSet();
            DataTable table1 
= new DataTable("table1");
            
for(int i = 0;i < result.Tables[0].Columns.Count;i++)
                table1.Columns.Add(result.Tables[
0].Rows[0][i].ToString(), typeof(string));
            
for(int i = 1;i < result.Tables[0].Rows.Count;i++)
            {
                DataRow dr 
= table1.NewRow();
                
for(int j = 0;j < result.Tables[0].Columns.Count;j++)
                {
                    dr[j] 
= result.Tables[0].Rows[i][j].ToString();
                }
                table1.Rows.Add(dr);
            }
            ds.Tables.Add(table1);
            
return new object[]{ds, filePath}; 
        }
posted on 2006-08-24 11:56  冰羽  阅读(234)  评论(0)    收藏  举报