国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#操作NPOI導入導出

2019-11-14 14:11:49
字體:
來源:轉載
供稿:網友
//把T_Seats中的輸入導出到Excel        PRivate void button3_Click(object sender, EventArgs e)        {            //1.讀取            string sql = "select * from T_Seats";            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))            {                if (reader.HasRows)                {                    //創建Workbook                    IWorkbook wk = new HSSFWorkbook();                    //創建Sheet                    ISheet sheet = wk.CreateSheet("T_Seats");                    int rowIndex = 0;                    #region 讀取并創建每一行                    //讀取每一條數據                    while (reader.Read())                    {                        //CC_AutoId, CC_LoginId, CC_LoginPassWord, CC_UserName, CC_ErrorTimes, CC_LockDateTime, CC_TestInt                        int autoId = reader.GetInt32(0);                        string uid = reader.GetString(1);                        string pwd = reader.GetString(2);                        string name = reader.GetString(3);                        int errorTimes = reader.GetInt32(4);                        DateTime? lockDate = reader.IsDBNull(5) ? null : (DateTime?)reader.GetDateTime(5);                        int? testInt = reader.IsDBNull(6) ? null : (int?)reader.GetInt32(6);                        IRow row = sheet.CreateRow(rowIndex);                        rowIndex++;                        //像行中創建單元格                        row.CreateCell(0).SetCellValue(autoId);                        row.CreateCell(1).SetCellValue(uid);                        row.CreateCell(2).SetCellValue(pwd);                        row.CreateCell(3).SetCellValue(name);                        row.CreateCell(4).SetCellValue(errorTimes);                        //對于數據庫中的空值,向單元格中插入空內容                        ICell cellLockDate = row.CreateCell(5);                        if (lockDate == null)                        {                            //設置單元格的數據類型為Blank,表示空單元格                            cellLockDate.SetCellType(CellType.BLANK);                        }                        else                        {                            cellLockDate.SetCellValue((DateTime)lockDate);                            //創建一個單元格格式對象                            ICellStyle cellStyle = wk.CreateCellStyle();                            cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");                            //設置當前日期這個單元格的是CellStyle屬性                            cellLockDate.CellStyle = cellStyle;                        }                        ICell cellTestInt = row.CreateCell(6);                        if (testInt == null)                        {                            cellTestInt.SetCellType(CellType.BLANK);                        }                        else                        {                            cellTestInt.SetCellValue((int)testInt);                        }                    }                    #endregion                    //將Excel寫入文件                    using (FileStream fsWrite = File.OpenWrite("tseats.xls"))                    {                        wk.Write(fsWrite);                    }                }            }            MessageBox.Show("操作完畢!");            //2.寫Excel        }        //把Excel的內容導入到數據庫表T_Seats        private void button4_Click(object sender, EventArgs e)        {            using (FileStream fsRead = File.OpenRead("tseats.xls"))            {                //1.讀取Excel                IWorkbook wk = new HSSFWorkbook(fsRead);                ISheet sheet = wk.GetSheetAt(0);                string sql_insert = "insert into T_Seats values(@uid,@pwd,@uname,@errorTimes,@lockDate,@testint)";                //讀取sheet中的每一行                for (int r = 0; r <= sheet.LastRowNum; r++)                {                    //讀取每行                    IRow row = sheet.GetRow(r);                    //讀取除了第一列的其他幾列                    string loginId = row.GetCell(1).StringCellValue;                    string password = row.GetCell(2).StringCellValue;                    string username = row.GetCell(3).StringCellValue;                    int errorTimes = (int)row.GetCell(4).NumericCellValue;                    double? lockDate = null;                    ICell cellLockDate = row.GetCell(5);                    if (cellLockDate != null && cellLockDate.CellType != CellType.BLANK)                    {                        lockDate = row.GetCell(5).NumericCellValue;                    }                    else                    {                        //lockDate = null;                    }                    int? testInt = null;                    ICell cellTestInt = row.GetCell(6);                    if (cellTestInt != null && cellTestInt.CellType != CellType.BLANK)                    {                        testInt = (int)cellTestInt.NumericCellValue;                    }                    else                    {                        //testInt = null;                    }                    SqlParameter[] pms = new SqlParameter[] {                         new SqlParameter("@uid",loginId),                        new SqlParameter("@pwd",password),                        new SqlParameter("@uname",username),                        new SqlParameter("@errorTimes",errorTimes),                                             new SqlParameter("@lockDate",lockDate==null?DBNull.Value:(object)DateTime.FromOADate((double)lockDate)),                        new SqlParameter("@testint",testInt==null?DBNull.Value:(object)testInt),                    };                    //執行插入操作                    SqlHelper.ExecuteNonQuery(sql_insert, CommandType.Text, pms);                }            }            MessageBox.Show("ok");            //2.向表T_Seats執行insert語句        }    }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太谷县| 隆林| 峨眉山市| 涞水县| 广安市| 南昌市| 麻栗坡县| 南澳县| 锡林浩特市| 偃师市| 玉林市| 宜城市| 綦江县| 衡阳县| 凯里市| 长兴县| 视频| 德阳市| 鸡西市| 陵水| 宜黄县| 米林县| 诸暨市| 临安市| 来凤县| 舟曲县| 麻栗坡县| 永平县| 永宁县| 达尔| 遵义市| 顺昌县| 阳泉市| 西乡县| 长兴县| 许昌市| 洪泽县| 许昌市| 双柏县| 贵德县| 扬州市|