• NPOI导出


    image
    数据库大概如上,没有将全部数据导出来。只是3个字段。
    private void button1_Click(object sender, EventArgs e)
            {
                SaveFileDialog sdfExport = new SaveFileDialog();
                sdfExport.Filter = "Excel文件|*.xls";
                if (sdfExport.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string filename = sdfExport.FileName;
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("员工信息");
                IRow rowHeader = sheet.CreateRow(0);
                rowHeader.CreateCell(0, CellType.STRING).SetCellValue("员工姓名");//表头设置
                rowHeader.CreateCell(1, CellType.STRING).SetCellValue("员工电话");
                rowHeader.CreateCell(2, CellType.STRING).SetCellValue("提交时间");
                string conStr = "Data Source=.;Initial CataLog=HM3Data;User Id=sa;Password=qiufeng";
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    string sqlStr = "select * from UserInfo";
                    SqlDataAdapter da = new SqlDataAdapter(sqlStr, conStr);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataTable dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        IRow row = sheet.CreateRow(i + 1);
                        DataRow dr = dt.Rows[i];
                        row.CreateCell(0, CellType.STRING).SetCellValue(dr["UName"].ToString());
                        row.CreateCell(1, CellType.STRING).SetCellValue(dr["Phone"].ToString());
                        ICellStyle styledate = workbook.CreateCellStyle();//日期类型的格式
                        IDataFormat format = workbook.CreateDataFormat();
                        styledate.DataFormat = format.GetFormat("yyyy\"年\"m\"月\"d\"日\"");
                        ICell cell = row.CreateCell(2, CellType.NUMERIC);
                        cell.CellStyle = styledate;
                        cell.SetCellValue(Convert.ToDateTime(dr["SubTime"].ToString()));
    
                    }
    
                }
                using (Stream stream = File.OpenWrite(filename))
                {
                    workbook.Write(stream);
                }
    
            }
  • 相关阅读:
    maven的安装步骤
    Sublime Text 3设置指南
    Eclipse 不同版本名称
    redis命令简单介绍
    redis简介与window安装:
    redis数据类型:String
    Mybatis-plus的使用步骤
    SpringgBoot父子工程的创建
    我的大学-易班
    IDEA:配置Tomcat并运行应用
  • 原文地址:https://www.cnblogs.com/automation/p/2943451.html
Copyright © 2020-2023  润新知