111,097
社区成员




public static DataSet ReadExcel2003(string excelPath)
{
DataSet ds = new DataSet();
//string workName = "";
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = false;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(excelPath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码
//指定要操作的Sheet,两种方式:
Microsoft.Office.Interop.Excel.Sheets xSheets = xBook.Sheets;
for (int k = 1; k <= xSheets.Count; k++)
{
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xSheets[k];
//查找列数
int rowCount = 0;
for (int i = 1; i < xSheet.Rows.Count; i++)
{
if (((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[i, 1]).Value2 == null)
{
rowCount = i;
break;
}
}
//查找行数
int columnCount = 0;
for (int i = 1; i < xSheet.Columns.Count; i++)
{
if (((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[2, i]).Value2 == null)
{
columnCount = i;
break;
}
}
DataTable dt = new DataTable();
for (int i = 1; i < columnCount; i++)
{
string ss = ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[1, i]).Value2.ToString();
if (ss != "")
{
dt.Columns.Add(ss);
}
else
{
dt.Columns.Add("Column" + i);
}
}
for (int i = 2; i < rowCount; i++)
{
DataRow dr = dt.NewRow();
for (int j = 1; j < columnCount; j++)
{
dr[j - 1] = ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[i, j]).Value2 == null ? "" : ((Microsoft.Office.Interop.Excel.Range)xSheet.Cells[i, j]).Value2.ToString();
}
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
}
try
{
xBook.Save();
//Microsoft.Office.Interop.Excel.Worksheet xSheet2=(Microsoft.Office.Interop.Excel.Worksheet)xApp.ActiveSheet;
xBook.Close(false, false, Missing.Value);
xApp.Quit();
}
catch { }
IntPtr t = new IntPtr(xApp.Hwnd);
int m = 0;
GetWindowThreadProcessId(t, out m);
Process p = Process.GetProcessById(m);
p.Kill();
return ds;
}
/// <summary>
/// 读取一个连续区域的Cell的值(矩形区域,包含一行或一列,或多行,多列),返回一个一维字符串数组。
/// </summary>
/// <param name="StartCell">StartCell是要写入区域的左上角单元格</param>
/// <param name="EndCell">EndCell是要写入区域的右下角单元格</param>
/// <returns>值的集合</returns>
public string[] getCellsValue(string StartCell, string EndCell)
{
string[] sValue = null;
//try
//{
xlRange = (Excel.Range)xlSheet.get_Range(StartCell, EndCell);
sValue = new string[xlRange.Count];
int rowStartIndex = ((Excel.Range)xlSheet.get_Range(StartCell, StartCell)).Row; //起始行号
int columnStartIndex = ((Excel.Range)xlSheet.get_Range(StartCell, StartCell)).Column; //起始列号
int rowNum = xlRange.Rows.Count; //行数目
int columnNum = xlRange.Columns.Count; //列数目
int index = 0;
for (int i = rowStartIndex; i < rowStartIndex + rowNum; i++)
{
for (int j = columnStartIndex; j < columnNum + columnStartIndex; j++)
{
//读到空值null和读到空串""分别处理
sValue[index] = ((Excel.Range)xlSheet.Cells[i, j]).Text.ToString();
index++;
}
}
//}
//catch (Exception e)
//{
// CloseXlApp();
// throw new Exception(e.Message);
//}
return (sValue);
}