C# 操作NOPI 导入导出

//把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语句
        }
    }
时间: 2024-08-29 07:45:12

C# 操作NOPI 导入导出的相关文章

.net 自己写的操作Excel 导入导出 类(以供大家参考和自己查阅)

由于现在网页很多都关系到Excel 的操作问题,其中数据的导入导出更是频繁,作为一个菜鸟,收集网上零散的知识,自己整合,写了一个Excel导入到GridView ,以及将GridView的数据导出到EXCEL的类方法,以供参考和方便自己以后查阅. 1 #region 引用部分 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Dat

NOPI操作EXCEL导入导出

private void btnOutput_Click(object sender, EventArgs e) { List<MODEL.Classes> list = cm.GetClassInfo(false); //获取对象数据集合 HSSFWorkbook workbook=new HSSFWorkbook (); //新建Excel工作表 HSSFSheet sheet=workbook.CreateSheet("classes"); //在工作文档中新建页 f

c#操作excel导入导出时同时向用户表插入账户与密码

<><><><>本代码源于师兄<><><><><><> 1:首先需要在前端显示界面View视图中添加导入Excel和导出Excel按钮: @using (Html.BeginForm()) { } 这里注意,导出Excel是通过获取当下的表单的方式来导出数据的. 2:然后为导入Excel添加导入方法function: js部分以图片存放 3:添加点击事件后弹出来的操作界面(importe

【原创】POI操作Excel导入导出工具类ExcelUtil

关于本类线程安全性的解释: 多数工具方法不涉及共享变量问题,至于添加合并单元格方法addMergeArea,使用ThreadLocal变量存储合并数据,ThreadLocal内部借用Thread.ThreadLocalMap以当前ThreadLocal为key进行存储,设置一次变量,则其他线程也会有上次数据的残留,因此在addMergeArea方法中进行清空的操作.为了保证原子性, 采用ReentrantLock确保一次只有一个线程可以进行添加合并数据的操作. 线程安全性从以上两个方面保证. 水

NOPI导入导出

using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Data; using System.IO; using System.Windows.Forms; namespace Excel导出示例 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnIn_C

[csdn markdown]使用摘记二 快捷键及导入导出Markdown文件

csdn推出了新的编辑器markdown,对于习惯使用离线编辑的人员来说是个大的福利,比如上班的时候,不能联网,但是又有好些知识点需要记录,等到下班了呢,又想直接把这些排版格式良好的文件直接上传到csdn博客,那么好了,现在有了这个编辑器,使用它的导入Markdown文件 就可以了. 目录 目录 导入导出 菜单位置 导入操作 导出操作 快捷键 导入导出 菜单位置 点击这个按钮,出现导入对话框,注意,这个导入会全部覆盖当前编辑的文章,如果不是全部使用要导入的文件,请谨慎使用此操作. 导入操作 弹出

Powershell管理系列(二十六)PowerShell操作之批量导出&导入邮箱

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包,QQ:185426445.电话18666943750 项目中有时候做跨林邮箱迁移的时候,条件不成熟,比如安全考虑或者其他考虑,不能做双林信任,这样就提出了一个问题,历史邮件需要使用的话怎么办,一个简单高效的解决办法就是从源森林批量导出邮件为.pst文件,在批量导入到目的域森林,具体操作如下: 1.赋予管理账号邮件导入导出权限,命令如下: cls whoami New-Manageme

linux系统上Mysql数据库导入导出操作

需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作均在终端命令行下进行 1.mysqldump -u root -p dz > /home/dz_bak.sql        #导出数据库     123456     #输入数据库密码     扩展:     mysqldump -u root -p dz pre_portal_comment >

ORACLE 导入导出操作

1.导入命令: imp userId/[email protected] full=y  file=D:\data\xxx.dmp ignore=y 2.导出命令 exp userId/[email protected] file=d:\dkj\test.dmp tables=(wf_test) exp userId/[email protected] buffer=50000000 file=D:\data\xxx.dmp owner=userId 1.在安装完ORACLE 11g后,在sql