文件导出成功

作者: www.9159.com  发布:2019-11-06

   //将数据写到硬盘
            using (FileStream saveurl = File.OpenWrite(@"C:UsersAdministratorDesktop112.xls"))
            {
                workbook.Write(saveurl);
                Title = "已经导出数据!";
            }
        }
        else
        {
            Title = "没有导出任何数据!";
        }

——————————分享End——————————

}

————————————————DataTable END————————————————

 

result = true;

————————————————DataTable Star————————————————
        DataTable dt = SqlHelper.ExecuteDataTable(sql);
        if (dt.Rows.Count > 0)
        {
            //创建工作簿
            IWorkbook workbook = new HSSFWorkbook();
            //创建表
            ISheet sheet = workbook.CreateSheet("DBToExcel");
            IRow row0 = sheet.CreateRow(0);
            row0.CreateCell(0).SetCellValue("id信息");
            row0.CreateCell(1).SetCellValue("名称信息");
            row0.CreateCell(2).SetCellValue("备注信息");
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                //创建行接受DataTable的行数据
                IRow row = sheet.CreateRow(r + 1);
                row.CreateCell(0).SetCellValue((int)dt.Rows[r]["Id"]);
                row.CreateCell(1).SetCellValue(dt.Rows[r]["Name"].ToString());
                row.CreateCell(2).SetCellValue(dt.Rows[r]["Remarks"].ToString());

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;

dt = dataTable;

    string sql = @"select * from T_Excel";

  List<UserInfo> listUser = new List<UserInfo>()
           {
               new UserInfo { name="1", id="1", phone="1r" },
               new UserInfo { name="2", id="2", phone="2r" },
               new UserInfo { name="3", id="3", phone="3r" },
               new UserInfo { name="4", id="4", phone="4r" },
               new UserInfo { name="5", id="5", phone="5r" },
           };
        1、//创建工作簿对象
       IWorkbook workbook = new HSSFWorkbook();
        2、//创建工作表
        ISheet sheet = workbook.CreateSheet("onesheet");
        IRow row0 = sheet.CreateRow(0);
        row0.CreateCell(0).SetCellValue("用户Id");
        row0.CreateCell(1).SetCellValue("用户名称");
        row0.CreateCell(2).SetCellValue("用户备注信息");
        for (int r = 1; r < listUser.Count; r++)
        {
            3、//创建行row
            IRow row = sheet.CreateRow(r);
            row.CreateCell(0).SetCellValue(listUser[r].id);
            row.CreateCell(1).SetCellValue(listUser[r].name);
            row.CreateCell(2).SetCellValue(listUser[r].phone);
            row.CreateCell(3).SetCellValue(listUser[r].pwd);
        }

int columnCount = dt.Columns.Count;

————————————————SqlDataReader star ————————————————
            SqlDataReader reader = SqlHelper.ExecuteReader(sql);
            if (reader.HasRows)
            {
               //创建工作簿
                IWorkbook workbook = new HSSFWorkbook();
                //创建工作表
               ISheet sheet = workbook.CreateSheet("DBToExcel");
                int rowIndex = 0;
                while (reader.Read())
                {
                    //读取一条数据就创建一行row
www.9159.com ,                    IRow row = sheet.CreateRow(rowIndex);
                    //记录读取数据行数
                    rowIndex++;
                    row.CreateCell(0).SetCellValue(reader.GetInt32(0));
                    row.CreateCell(1).SetCellValue(reader.GetString(1));
                    row.CreateCell(2).SetCellValue(reader.GetString(2));
               }

    //导出Excel文件
            workbook.Write(url);
            Response.Write("<script>alert('写入成功!')</script>");
        };

MessageBox.Show(ex.Message);

    using (FileStream saveurl = File.OpenWrite(@"C:UsersAdministratorDesktop112.xls"))
            {
                workbook.Write(saveurl);
                Title = "已经导出数据!";
            }
        }
        else
        {
            Title = "没有导出任何数据!";
        }

    //创建流对象并设置存储Excel文件的路径
        using (FileStream url = File.OpenWrite(@"C:UsersAdministratorDesktop写入excel.xls"))
        {

DataTable dt = new DataTable();

 ————————————————SqlDataReader  end————————————————

将List集合的数据写到一个Excel文件并导出示例:

}

            }

catch (Exception ex)

{

FileStream stm = null;

catch (IOException ex)

//创建表

ISheet sheet = null;

return false;

}

IWorkbook wb = null;

row = sheet.CreateRow(i + 1);

     using System.Collections;

if (stm != null)

}

{

for (int j = 0; j < columnCount; j++)

}

wb = new HSSFWorkbook();

return result;

cell.SetCellValue(dt.Columns[c].ColumnName);

for (int c = 0; c < columnCount; c++)

using (stm = File.OpenWrite(excename + ".xls"))

     using System.IO;

for (int i = 0; i < rowCount; i++)

2、using NPOI.SS.UserModel;

{

bool result = false;

}

3、导出excel文件:

{

//设置每行每列的单元格

//设置列头

cell.SetCellValue(dt.Rows[i][j].ToString());

{

wb.Write(stm);

1、首先需要添加引用:NPOI.dll文件

}

MessageBox.Show("提示:文件导出成功!");

}

int rowCount = dt.Rows.Count;

try

stm.Close();

{

{

cell = row.CreateCell(c);

{

{

try

}

sheet = wb.CreateSheet("Sheet0");

private bool exportExcel(string excename,DataTable dataTable)

IRow row = sheet.CreateRow(0);

}

cell = row.CreateCell(j);//excel第二行开始写入数据

{

     using NPOI.HSSF.UserModel;

ICell cell = null;

本文由9159.com发布于www.9159.com,转载请注明出处:文件导出成功

关键词: