码迷,mamicode.com
首页 > 数据库 > 详细

NPOI将Excel中的数据导入到数据库

时间:2014-10-22 12:29:50      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

    using(FileStream fs=File.OpenRead(@"d:/MY.xls"))
            {
                IWorkbook wk = new HSSFWorkbook(fs);
                if (wk.NumberOfSheets>0)
                {
                    ISheet sheet = wk.GetSheetAt(0);
                    for (int i = 0; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);

                        // //Id, Name, ParentID
                        //string name=row.GetCell(1).CellType==CellType.STRING.StringCellValue;
                        string Name = row.GetCell(1).StringCellValue == "null" ? null : row.GetCell(1).StringCellValue;
                        int? ParentID = row.GetCell(2) == null ? null:(int?) row.GetCell(2).NumericCellValue;

                        string sql = "insert into TreeView values( @Name, @ParentID)";

                        SqlParameter[] pms = new SqlParameter[]{
                        new SqlParameter("@Name",Name==null?DBNull.Value:(object)Name),
                        new SqlParameter("@ParentID",ParentID==null?DBNull.Value:(object)ParentID)

                        };
                        SqlHelper.ExecteNonQuery(System.Data.CommandType.Text, sql, pms);
                    }
                }
                
            }
bubuko.com,布布扣
  public class DoExcel
    {


        public static void LogeToExcel(string[] strs)
        {
            DateTime dt = DateTime.Now;
            string dir = "/ApiLogs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
            string path = HttpContext.Current.Server.MapPath(dir);
            int rowNum = 0;
            if (!File.Exists(path))
            {
                //Directory.CreateDirectory(path);
                //创建一个workbook
                IWorkbook wk = new HSSFWorkbook();
                //创建sheet
                ISheet sh = wk.CreateSheet("Sheet1");
                IRow row = sh.CreateRow(rowNum);
                for (int i = 0; i < strs.Length; i++)
                {
                    ICell iCell = row.CreateCell(i);
                    iCell.SetCellValue(strs[i]);
                }

                using (FileStream fs = File.OpenWrite(path))
                {
                    wk.Write(fs);
                }
            }
            else
            {
                FileStream fss = File.OpenRead(path);
                IWorkbook wk = new HSSFWorkbook(fss);
                int InsertRowIndex = wk.GetSheetAt(0).LastRowNum+1;
                ExportExcelByTemple(strs, path, path, 20, InsertRowIndex);
            }
        }

        /// <summary>
        /// 用模板导出Excel
        /// </summary>
        /// <param name="table"></param>
        /// <param name="strFileName">导出路径</param>
        /// <param name="templetPath">模板路径</param>
        /// <param name="startRow">从第几行开始写数据,从1开始</param>
        public static void ExportExcelByTemple(string[] strs, string strFileName, string templetPath, int rowHeight, int startRow)
        {
            try
            {
                HSSFWorkbook workbook = getWorkBook(templetPath);
                ISheet sheet = getSheet(workbook);
                writeData(workbook, sheet, strs, strFileName, rowHeight, startRow);
                saveData(workbook, strFileName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 解析Excel模板,返回WorkBook
        /// </summary>
        /// <param name="templetPath"></param>
        /// <returns></returns>
        private static HSSFWorkbook getWorkBook(string templetPath)
        {
            FileStream file = new FileStream(templetPath, FileMode.Open, FileAccess.Read);
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            return workbook;
        }

        /// <summary>
        /// 返回Sheet
        /// </summary>
        /// <param name="workbook"></param>
        /// <returns></returns>
        private static ISheet getSheet(HSSFWorkbook workbook)
        {
            return workbook.GetSheetAt(0);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="sheet"></param>
        /// <param name="dtSource"></param>
        /// <param name="strFileName"></param>
        /// <param name="rowHeight"></param>
        /// <param name="startRow"></param>
        /// <param name="size"></param>
        private static void writeData(HSSFWorkbook workbook, ISheet sheet, string[] strs, string strFileName, int rowHeight, int startRow)
        {
            // //填充表头     
            IRow dataRow = new HSSFRow();
            //填充内容    
            dataRow = sheet.CreateRow(startRow);
            dataRow.Height = (short)(rowHeight * 20);
            for (int j = 0; j < strs.Length; j++)
            {
                string drValue = strs[j];
                dataRow.CreateCell(j).SetCellValue(drValue);
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="strFileName"></param>
        private static void saveData(HSSFWorkbook workbook, string strFileName)
        {
            //保存     
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
        }
    }
向已有的Excel中添加一条信息

 

  

NPOI将Excel中的数据导入到数据库

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/taidi/p/3896192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!