110,567
社区成员
发帖
与我相关
我的任务
分享
//上面代码测试了,是对的
//对比一下你的连接字串,双引号里面可用单引号
//test.xls中有3行3列
using(OleDbConnection cn =new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath +"\\test.xls" + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"))
{
try
{
string strSql="select * from [sheet1$]";
//OleDbCommand cmd = new OleDbCommand(strSql, cn);
OleDbDataAdapter da = new OleDbDataAdapter(strSql, cn);
//DataSet ds = new DataSet();
//da.Fill(ds);
//DataTable dt = ds.Tables[0];
//如果只有一个DataTable 可用下面的
DataTable dt = new DataTable();
da.Fill(dt);
string strTest = dt.Rows[1][1].ToString();
}
catch(Exception ex)
{
}
}
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Open(@"E:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel1.Visible = true;
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(XlWBATemplate.xlWBATWorksheet或true);
worksheet1.Activate();//激活sheet1表
excel1.Visible = true;
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
worksheet1 = (Worksheet)workbook1.Worksheets.Add(Type.Missing, workbook1.Worksheets[1], 1, Type.Missing);
worksheet1.Activate();
worksheet1.Cells[2, 2] = 3455555;
excel1.Visible = true;
excel1.DisplayAlerts = false;//不显示提示框
workbook1.Close(true, "d:\\1.xls", null);
//关闭
worksheet1 = null;
workbook1 = null;
excel1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
excel1 = null;
System.GC.Collect();