public void OpenExcel(string strFileName)
{
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
if (excel == null)
{
}
else
{
excel.Visible = false; excel.UserControl = true;
// 以只读的形式打开EXCEL文件
Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(2);
//取得总记录行数 (包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
//int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
//取得数据范围区域 (不包括标题列)
Range address = ws.Cells.get_Range("D2", "D" + rowsint);
Range name = ws.Cells.get_Range("J2", "J" + rowsint);
Range stype = ws.Cells.get_Range("C2", "C" + rowsint);
object[,] addressItem1 = (object[,])address.Value2;
object[,] nameCus1 = (object[,])name.Value2;
object[,] type1 = (object[,])stype.Value2;
addressItem = new string[rowsint, 1];
nameCus = new string[rowsint, 1];
type = new string[rowsint, 1];
for (int i = 1; i < 366; i++)
{
try
{
if (!string.IsNullOrEmpty(addressItem1[i, 1].ToString()))
{
addressItem[i - 1, 0] = addressItem1[i, 1].ToString();
nameCus[i - 1, 0] = nameCus1[i, 1].ToString();
type[i - 1, 0] = type1[i, 1].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
excel.Quit(); excel = null;
Process[] procs = Process.GetProcessesByName("excel");
foreach (Process pro in procs)
{
pro.Kill();//没有更好的方法,只有杀掉进程
}
GC.Collect();
}