如何用VS2012 C#对excel导入和导出,多谢大家了!

ken2002 2014-02-07 10:38:39

想实现用VS2012 C#对excel导入和导出,该Excel有合并行和列,有什么好方法实现呢,谢谢大家了!
...全文
780 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ken2002 2014-02-12
  • 打赏
  • 举报
回复
多谢大家了!
火星大能猫 2014-02-08
  • 打赏
  • 举报
回复
可以考虑使用myxls和NPOI 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;
        }

Regan-lin 2014-02-07
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_4ce36a78010161k9.html
ken2002 2014-02-07
  • 打赏
  • 举报
回复
To wg5945 多谢你了,有没有具体一点的例子呢,因为现在只是初学 还有支持excel2003和2007么,谢谢你了!
wg5945 2014-02-07
  • 打赏
  • 举报
回复

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧