Microsoft.Office.Interop.Excel 操作excel到DataTable中
来源:互联网 发布:软件著作权申请费用: 编辑:程序博客网 时间:2024/05/18 00:34
方法如下:
/// <summary>
/// Microsoft.Office.Interop.Excel 操作excel到Datatable中/// </summary>
public System.Data.DataTable GetExcelTable(string filename)
{
object missing = System.Reflection.Missing.Value;
Application myExcel = new Application();//lauch excel application
if (myExcel == null)
{
return null;
}
else
{
myExcel.Visible = false; myExcel.UserControl = true;
Workbook myBook = myExcel.Application.Workbooks.Open(filename, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
if (myBook != null) //打开成功
{
myExcel.Visible = false;
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1]; //得到工作表
System.Data.DataTable dt = new System.Data.DataTable();
for (int j = 1; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
dt.Columns.Add();
for (int i = 1; i <= mySheet.Cells.CurrentRegion.Rows.Count; i++) //把工作表导入DataTable中
{
DataRow myRow = dt.NewRow();
for (int j = 1; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
{
Excel.Range temp = (Excel.Range)mySheet.Cells[i, j];
string strValue = temp.Text.ToString();
myRow[j - 1] = strValue;
}
dt.Rows.Add(myRow);
}
myExcel.Quit(); //退出Excel文件
myExcel = null;
Process[] procs = Process.GetProcessesByName("excel");
foreach (Process pro in procs)
{
pro.Kill();//杀掉进程
}
GC.Collect();
return dt;
}
}
//打开不成功
return null;
}
- Microsoft.Office.Interop.Excel 操作excel到DataTable中
- 引用Microsoft.Office.Interop.Excel.dll操作Excel常用方法
- 【转载】Excel操作 Microsoft.Office.Interop.Excel.dll的使用
- “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用
- 关于Microsoft.Office.Interop.Excel的一些简单操作
- vs2005 中找不到Microsoft.Office.Interop.Excel引用
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
- 引用Microsoft.Office.Interop.Excel的解决方法
- 求解Microsoft.Office.Interop.Excel.Application引用
- ” Microsoft.Office.Interop.Excel”无法引用
- Microsoft.Office.Interop.Excel的用法
- Microsoft.Office.Interop.Excel的用法
- Microsoft.Office.Interop.Excel的用法
- Microsoft.Office.Interop.Excel的用法
- Microsoft.Office.Interop.Excel列宽自适应
- NPOI读写Excel 或 Microsoft.Office.Interop.Excel 读取excel
- [转]VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Excel、VBIDE
- myeclipse快捷键配置
- 位运算中的异或运算
- 学习正则表达式(三)
- 【转】PDF转换成Word文档
- 配置lustre和rsh时遇到的小问题
- Microsoft.Office.Interop.Excel 操作excel到DataTable中
- jQuery学习笔记二
- 【转】教你怎样解除电脑开机密码
- C# 操作0001”流水号+1到“0002”
- 带数据结构试验时的一些感想
- 【转】简历的写法
- FFmpeg情景分析 1. FFmpeg转码框架解析
- 【转】策划书的写法
- updatepanel 中使用JS alert方法