public System.Data.DataTable getExcelTable(string filename, int sheetNum)
{ object missing = Type.Missing.ToString();
Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Application.Workbooks.Open(path); //打开Excel文件
if (myBook != null) //打开成功
{
myExcel.Visible = false; Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[sheetNum]; //得到工作表 for (int i = 2; i <= mySheet.Cells.CurrentRegion.Rows.Count; i++) //把工作表导入DataTable中
{
DataRow myRow = dtjbzl.NewRow(); //dtjbzl 是我原始的datable for (int j = 1; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
{
try
{
Microsoft.Office.Interop.Excel.Range temp = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells[i, j];
string strValue = temp.Text.ToString();
myRow[j - 1] = strValue;
}
catch
{
}
}
dtjbzl.Rows.Add(myRow);
}
myExcel.Quit(); //退出Excel文件 System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
} return dtjbzl;
} else
{
//打开不成功
return null;
}
}
以上的代码是我想将EXCEL中的数据加到dtjbzl这个datable中,可是有时候 运行到
Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Application.Workbooks.Open(path); //打开Excel文件
这句就提示 “rpc服务器不可用”,当我重启窗体在调这函数有没问题了,我确定我的打开管理工具→服务→找到Remote Procedure Call (RPC) 已经启动。求高手指教啊Excel
{ object missing = Type.Missing.ToString();
Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Application.Workbooks.Open(path); //打开Excel文件
if (myBook != null) //打开成功
{
myExcel.Visible = false; Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[sheetNum]; //得到工作表 for (int i = 2; i <= mySheet.Cells.CurrentRegion.Rows.Count; i++) //把工作表导入DataTable中
{
DataRow myRow = dtjbzl.NewRow(); //dtjbzl 是我原始的datable for (int j = 1; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
{
try
{
Microsoft.Office.Interop.Excel.Range temp = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells[i, j];
string strValue = temp.Text.ToString();
myRow[j - 1] = strValue;
}
catch
{
}
}
dtjbzl.Rows.Add(myRow);
}
myExcel.Quit(); //退出Excel文件 System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
} return dtjbzl;
} else
{
//打开不成功
return null;
}
}
以上的代码是我想将EXCEL中的数据加到dtjbzl这个datable中,可是有时候 运行到
Microsoft.Office.Interop.Excel.Workbook myBook = myExcel.Application.Workbooks.Open(path); //打开Excel文件
这句就提示 “rpc服务器不可用”,当我重启窗体在调这函数有没问题了,我确定我的打开管理工具→服务→找到Remote Procedure Call (RPC) 已经启动。求高手指教啊Excel
打开:
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(connStr);
列举表名:
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
List<String> sheets = (from t in dtSheetName.AsEnumerable()
where t["TABLE_TYPE"].ToString() == "TABLE" && reg.IsMatch(t["TABLE_NAME"].ToString())
select t["TABLE_NAME"].ToString()).ToList();foreach (string sheet in sheets)
{
string sql = String.Format("select * from [{0}]", sheet);
OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
oda.Fill(dt);
///在这儿开始你的操作
}