Excel_To_DataTable
/// <summary>
/// Read data in excel file to datatable
/// </summary>
/// <param name="filename">Excel file name</param>
/// <param name="sheetNum">Which sheet to read</param>
/// <returns></returns>
public System.Data.DataTable GetExcelTable(string filename, int sheetNum)
{
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
object missing = Missing.Value;
Workbook myBook = myExcel.Application.Workbooks.Open(filename, missing, missing, missing,
missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing); //open excel file if (myBook != null)
{
myExcel.Visible = false; Worksheet mySheet = (Worksheet)myBook.Worksheets[sheetNum];
System.Data.DataTable dt = new System.Data.DataTable(); for (int j = ; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
dt.Columns.Add(); for (int i = ; i <= mySheet.Cells.CurrentRegion.Rows.Count; i++)
{
DataRow myRow = dt.NewRow(); for (int j = ; j <= mySheet.Cells.CurrentRegion.Columns.Count; j++)
{
Microsoft.Office.Interop.Excel.Range temp = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells[i, j];
string strValue = temp.Text.ToString();
myRow[j - ] = strValue;
}
dt.Rows.Add(myRow);
}
myExcel.Quit(); //Kill excel process
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
} return dt;
} return null;
}
Excel_To_DataTable的更多相关文章
随机推荐
- Jmeter(七)Mongodb的增删改查
1.启动JMeter,新建线程组,设置线程组属性 2.右键添加-MongoDB Source Config 设置属性Server Address List:192.168.0.99 MongoDB S ...
- Linux修改信息
修改时间 sudo date -s MM/DD/YY //修改日期 sudo date -s hh:mm:ss //修改时间 在修改时间以后,修改硬件CMOS的时间 sudo hwclock --sy ...
- PAT 1077 Kuchiguse [一般]
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- C语言中const和数组
C语言中const的用法 const:在定义变量时,如果使用关键字const,那就表示限制这个变量值不允许被改变. (1) 修饰变量 const离谁越近,谁的值就不能改变. int const ...
- jenkins git gradle android自动化构建配置
需要安装软件(jenkins所在服务器): gradle.SDK 一.gradle安装(服务器部署的gradle版本需要等于或高于开发环境的gradle版本) 1.下载地址:http://servic ...
- 取MAC地址 (含多网卡),最好的方法,支持Vista,Win7
取MAC地址 (含多网卡),最好的方法,支持Vista,Win7 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Cl ...
- Delphi APP 開發入門(一)重生的 Delphi
Delphi APP 開發入門(一)重生的 Delphi 分享: Share on facebookShare on twitterShare on google_plusone_share 閲讀 ...
- 技术分享会(二):SQLSERVER索引介绍
SQLSERVER索引介绍 一.SQLSERVER索引类型? 1.聚集索引: 2.非聚集索引: 3.包含索引: 4.列存储索引: 5.无索引(堆表): 二.如何创建索引? 索引示例: 建表 creat ...
- Leetcode 235
思路1:对于一棵二叉排序树 1.如果当前节点的值小于p,q的值,那么LCA一定在root的右边: 2.如果当前节点的值大于p,q的值,那么LCA一定在root的左边: 3.如果当前节点的值在p,q的值 ...
- matplotlib.pyplot 让数据可视化
1.条形图 import matplotlib.pyplot as plt plt.style.use('ggplot') # 使用ggplot样式来模拟ggplot2风格的图形,ggplot2是一个 ...