如何用VS2012 C#对excel导入和导出,多谢大家了! http://tonyqus.sinaapp.com/npoi2tutorial 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/xuenzhen123/article/details/4713019 To wg5945多谢你了,有没有具体一点的例子呢,因为现在只是初学还有支持excel2003和2007么,谢谢你了! http://blog.sina.com.cn/s/blog_4ce36a78010161k9.html 可以考虑使用myxls和NPOINPOI还支持2007以上的格式.简单的excel读取 /// <summary> /// 读取excel /// </summary> /// <param name="fileName"></param> /// <returns></returns> private DataTable LoadExcel(string fileName) { int iColumnsCount = 0;//所需要的列的数量判断,用来判断excel格式是否正确. dtXls = new DataTable(); int iCodeIndex = 0, iNameIndex = 0, iSpecIndex = 0, iUnitIndex = 0; using (FileStream fs = new FileStream(fileName, FileMode.Open)) { HSSFWorkbook workbook = new HSSFWorkbook(fs); ISheet sheet = workbook.GetSheetAt(0);//获取表1 System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); if (rows.MoveNext())//读取列名 { HSSFRow row = (HSSFRow)(rows.Current); for (int i = 0; i < row.LastCellNum; i++)//遍历行的每一个单元格 { if (row.Cells[i] != null) { string strName = row.Cells[i].ToString().Trim(); switch (strName) { case "代码": iCodeIndex = i; dtXls.Columns.Add(strName); iColumnsCount++; break; case "全名": iNameIndex = i; dtXls.Columns.Add(strName); iColumnsCount++; break; case "规格型号": iSpecIndex = i; dtXls.Columns.Add(strName); iColumnsCount++; break; case "基本计量单位": iUnitIndex = i; dtXls.Columns.Add(strName); iColumnsCount++; break; } } } } dtXls.Columns.Add("类别"); if (iColumnsCount != 4) { MessageBox.Show("无法获取所需的excel数据!"); return null; } //读取内容 while (rows.MoveNext()) { DataRow dr = dtXls.NewRow(); HSSFRow row = (HSSFRow)rows.Current; ICell cellCode = row.GetCell(iCodeIndex); ICell cellName = row.GetCell(iNameIndex); ICell cellSpec = row.GetCell(iSpecIndex); ICell cellUnit = row.GetCell(iUnitIndex); if (cellCode == null) { dr[0] = ""; continue; } else { if ((rdoycl.Checked && !isMathcCode(cellCode.ToString()))||(rdozj.Checked && !isMatchTecCode(cellCode.ToString()))) { continue; } dr[0] = cellCode.ToString(); } if (cellName == null) dr[1] = ""; else dr[1] = cellName.ToString(); if (cellSpec == null) dr[2] = ""; else dr[2] = cellSpec.ToString(); if (cellUnit == null) dr[3] = ""; else dr[3] = cellUnit.ToString(); dr[4] = GetTypeNameById(dr[0].ToString()); dtXls.Rows.Add(dr); } } return dtXls; } 求助, 这段 XAML 为什么会报异常? 请看下Asp。net中的方法重写问题 谁能给解释一下,这是怎么回事? 如果一颗树,他的分支是不确定层也是不确定的,这种树... 如何在菜单上加上图标 请问一个关于Access数据库链接的问题 datagrid 隐藏列 dbgrid绑定,删除问题,比较简单,来拿分哦?????????????? richtextbox设置每行字体后新增内容字体问题 求助!用C#产生10个不重复的随机数怎么做? 有限图片识别 C# 如何检测WIN8用户正在使用麦克风,就是对着麦克风吹气
NPOI还支持2007以上的格式.简单的excel读取 /// <summary>
/// 读取excel
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private DataTable LoadExcel(string fileName)
{
int iColumnsCount = 0;//所需要的列的数量判断,用来判断excel格式是否正确.
dtXls = new DataTable();
int iCodeIndex = 0, iNameIndex = 0, iSpecIndex = 0, iUnitIndex = 0;
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
HSSFWorkbook workbook = new HSSFWorkbook(fs);
ISheet sheet = workbook.GetSheetAt(0);//获取表1
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
if (rows.MoveNext())//读取列名
{
HSSFRow row = (HSSFRow)(rows.Current);
for (int i = 0; i < row.LastCellNum; i++)//遍历行的每一个单元格
{
if (row.Cells[i] != null)
{
string strName = row.Cells[i].ToString().Trim();
switch (strName)
{
case "代码":
iCodeIndex = i;
dtXls.Columns.Add(strName);
iColumnsCount++;
break;
case "全名":
iNameIndex = i;
dtXls.Columns.Add(strName);
iColumnsCount++;
break;
case "规格型号":
iSpecIndex = i;
dtXls.Columns.Add(strName);
iColumnsCount++;
break;
case "基本计量单位":
iUnitIndex = i;
dtXls.Columns.Add(strName);
iColumnsCount++;
break;
}
}
}
} dtXls.Columns.Add("类别"); if (iColumnsCount != 4)
{
MessageBox.Show("无法获取所需的excel数据!");
return null;
}
//读取内容
while (rows.MoveNext())
{
DataRow dr = dtXls.NewRow();
HSSFRow row = (HSSFRow)rows.Current;
ICell cellCode = row.GetCell(iCodeIndex);
ICell cellName = row.GetCell(iNameIndex);
ICell cellSpec = row.GetCell(iSpecIndex);
ICell cellUnit = row.GetCell(iUnitIndex); if (cellCode == null)
{
dr[0] = "";
continue;
}
else
{
if ((rdoycl.Checked && !isMathcCode(cellCode.ToString()))||(rdozj.Checked && !isMatchTecCode(cellCode.ToString())))
{
continue;
}
dr[0] = cellCode.ToString();
} if (cellName == null)
dr[1] = "";
else
dr[1] = cellName.ToString();
if (cellSpec == null)
dr[2] = "";
else
dr[2] = cellSpec.ToString(); if (cellUnit == null)
dr[3] = "";
else
dr[3] = cellUnit.ToString();
dr[4] = GetTypeNameById(dr[0].ToString());
dtXls.Rows.Add(dr);
} }
return dtXls;
}