同上  希望给点代码啊

解决方案 »

  1.   

    这是我将Excel另存为Txt取文本的代码,自己看看吧 
    public static string GetExcel(string excelname)
            {
                try
                {
                    object Nothing = System.Reflection.Missing.Value;
                    string toname = excelname + ".txt";
                    /*Excel.ApplicationClass getexc = new Excel.ApplicationClass();
                    Type ExcelType = getexc.GetType();
                    Excel.Workbook Exceltemp = getexc.WorkbookActivate;
                    Type Exceltype = Exceltemp.GetType();
                    Excel.Workbook exc = (Excel.Workbook)Exceltype.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, doctemp, new Object[] { excelname, true, true });
                    Type excType = exc.GetType();
                    excType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { toname, .WdSaveFormat.wdFormatText });
                    ExcelType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, getwor, null);*/
                    Excel.Application exc = new Excel.Application();
                    exc.Workbooks.Open(excelname, true, 2, 5, Nothing, Nothing, true, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                    exc.ActiveWorkbook.SaveAs(toname, Excel.XlFileFormat.xlUnicodeText, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
                    exc.ActiveWorkbook.Close(false, toname, false);
                    //exc.ActiveWindow.Close(false, toname, false);
                    
                    //exc.Application.Quit();
                    exc.Workbooks.Close();
                    exc.Quit();
                    
                    exc = null;
                    GC.Collect();
                    return creatlogs.GetTxt(toname);
                    
                }
                catch(Exception e)
                {
                    creatlogs.Eventlog(excelname.ToString(), e.Message + "#excel文本读取失败");
                    return "";
                }
                
            }
      

  2.   

    读出excel中的sheet
    public static DataTable ImportExcel(string fileName)
    {
    string xlsDriver = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;";
    OleDbConnection cn = new OleDbConnection( string.Format(xlsDriver, fileName) );
    cn.Open();

    try
    {
    DataTable schema   = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
    //下面取得第一个表名
    string tableName   = schema.Rows[0]["TABLE_NAME"].ToString(); OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + tableName + "]",cn);
    DataSet ds = new DataSet();
    da.Fill(ds); da.Dispose();
    cn.Dispose(); return ds.Tables[0];
    }
    catch(Exception ex)
    {
    MessageBox.Show("对不起,数据导入出错!\\n" + ex.Message,"出错",MessageBoxButtons.OK, MessageBoxIcon.Error);
    return null;
    }
    finally
    {
    if (cn != null) cn.Dispose();
    }
    }
      

  3.   

    参考
    http://blog.csdn.net/blog51/archive/2007/08/28/1761493.aspx