zoukankan      html  css  js  c++  java
  • test

    DataTable dt = new DataTable();
    DataColumn[] cols = new DataColumn[] {
    new DataColumn("name",typeof(string)),
    new DataColumn ("birthday",typeof(DateTime)),
    new DataColumn ("score",typeof(int))
    };
    dt.Columns.AddRange(cols);
    Random rnd = new Random();

    for (int i = 0; i < 5; i++)
    {
    DataRow row = dt.NewRow();
    object[] items = new object[] {
    "小明",
    DateTime.Now ,
    rnd.Next(100)
    };
    row.ItemArray = items;
    dt.Rows.Add(row);
    }

    LwNpoiHelper.RenderDataTableToExcel(dt, "TR.xls");

    public partial class LwNpoiHelper
    {
    public static Stream RenderDataTableToExcel(DataTable SourceTable)
    {
    HSSFWorkbook workbook = new HSSFWorkbook();
    MemoryStream ms = new MemoryStream();
    ISheet sheet = workbook.CreateSheet();
    IRow headerRow = sheet.CreateRow(0);

    // handling header.
    foreach (DataColumn column in SourceTable.Columns)
    headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);

    // handling value.
    int rowIndex = 1;

    foreach (DataRow row in SourceTable.Rows)
    {
    IRow dataRow = sheet.CreateRow(rowIndex);

    foreach (DataColumn column in SourceTable.Columns)
    {
    if (column.DataType == typeof(int))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((int)row[column]);
    }
    else if (column.DataType == typeof(float))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((float)row[column]);
    }
    else if (column.DataType == typeof(double))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((double)row[column]);
    }
    else if (column.DataType == typeof(Byte))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((byte)row[column]);
    }
    else if (column.DataType == typeof(UInt16))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((UInt16)row[column]);
    }
    else if (column.DataType == typeof(UInt32))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((UInt32)row[column]);
    }
    else if (column.DataType == typeof(UInt64))
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue((UInt64)row[column]);
    }
    else if (column.DataType == typeof(DateTime))
    {
    //dataRow.CreateCell(column.Ordinal).SetCellValue((DateTime)row[column]);

    IDataFormat dataformat = workbook.CreateDataFormat();
    ICellStyle style = workbook.CreateCellStyle();

    dataRow.CreateCell(column.Ordinal).SetCellValue((DateTime)row[column]);

    style.DataFormat = dataformat.GetFormat("yyyy-MM-dd");
    dataRow.GetCell(column.Ordinal).CellStyle = style;

    }
    else
    {
    dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
    }


    // dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
    }

    rowIndex++;
    }

    workbook.Write(ms);
    ms.Flush();
    ms.Position = 0;

    sheet = null;
    headerRow = null;
    workbook = null;

    return ms;
    }

  • 相关阅读:
    HUAWEI防火墙双出口据链路带宽负载分担
    HUAWEI防火墙双出口根据链路优先级主备备份
    HUAWEI防火墙双出口环境下私网用户通过NAPT访问Internet
    如何实现IP话机接入交换机?
    WLAN-AC+AP,动态负载均衡用户量,避免某一个AP负载过重
    WLAN-AC+AP射频一劳永逸的调优方式
    中大型企业有线无线用户统一接入(实施笔记)
    js获取当前时间,返回日期yyyy-MM-dd
    cookie和token都存在在请求头header中,有什么区别,为什么建议使用token?
    vue中cookie的使用——将cookie放在请求头header中
  • 原文地址:https://www.cnblogs.com/MiLu/p/6664872.html
Copyright © 2011-2022 走看看