读取excel文件的两个方法

来源:互联网 发布:c语言代码大全表 编辑:程序博客网 时间:2024/05/16 09:54

方法1:

 

ExcelObj.Visible =

 

 

FilePath, missing, missing, missing,

missing, missing, missing, missing, missing, missing, missing,

missing, missing);

 

 

 

{

 

{

datasheet = sheet;

 

}

}

 

System.

 

 

{

 

 

 

{

 

|| values.GetValue(i, 10).ToString().Length != 0

|| values.GetValue(i, 11).ToString().Length != 0)

{

strMisspelledQuires += values.GetValue(i, 1).ToString() +

}

 

 

}

 

 

}

ExcelObj.Quit();

ExcelObj =

 

Application ExcelObj = new ApplicationClass();false;object missing = Type.Missing;Workbook theWorkbook = ExcelObj.Workbooks._Open(Sheets sheets = theWorkbook.Worksheets;Worksheet datasheet = null;foreach (Worksheet sheet in sheets)if (sheet.Name == "zh-cn_6000_May07_S15")break;Range range = datasheet.get_Range("A1", "K8");Array values = (System.Array)(range.Formula);string strMisspelledQuires = string.Empty;if (null != values)int len1 = values.GetLength(0);int len2 = values.GetLength(1);for (int i = 2; i <= len1; i++)if (values.GetValue(i, 7).ToString().Length != 0"/r/n";null;//KillProcess("EXCEL");

 

 

string LogFileName = "SpellAlterationQuires.txt";try

{

 

{

 

sw.Write(strMisspelledQuires);

sw.Close();

}

 

if (File.Exists(LogFileName))StreamWriter sw = File.AppendText(LogFileName);else

{

 

{

 

 

sw.Write(strMisspelledQuires);

sw.Close();

}

}

 

}

 

{

 

}

 

try

{

 

 

conn.Open();

 

 

strExcel =

myCommand =

ds =

myCommand.Fill(ds,

conn.Close();

 

}

 

{

 

}

 

 

参考:http://community.csdn.net/Expert/TopicView3.asp?id=5391204

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;";OleDbConnection conn = new OleDbConnection(strConn);string strExcel = "";OleDbDataAdapter myCommand = null;"select * from [zh-cn_6000_May07_S15$]";new OleDbDataAdapter(strExcel, conn);new DataSet();"[zh-cn_6000_May07_S15$]"); catch (Exception e)Console.WriteLine(e.Message);
using (Stream stream = File.OpenWrite(LogFileName))Encoding encode = Encoding.GetEncoding("utf-8");StreamWriter sw = new StreamWriter(stream, encode);catch (IOException e)Console.WriteLine(e.Message); 方法2: