zoukankan      html  css  js  c++  java
  • Excel导出下拉框(导出下拉框较少的选项)

    一、导出下拉框较少的选项

     Dictionary<int, DataTable> dicDropList   key 下拉框列Index,value 下拉数据,为了支持一个sheet有多个下拉

    if (dicDropList != null)
    {
    foreach (var item in dicDropList)
    {
    CellRangeAddressList regions = new CellRangeAddressList(1, 65535, item.Key, item.Key);
    string[] temp = new string[item.Value.Rows.Count];
    int i = 0;
    foreach (DataRow dr in item.Value.Rows)
    {
    temp[i] = dr[0].ToString();//字段
    i++;
    }
    DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(temp);
    HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
    dataValidate.CreateErrorBox("输入不合法", "请输入下拉列表中的值。");
    sheet.AddValidationData(dataValidate);
    }
    }

    例子:

    /// <summary>
    ///
    /// </summary>
    /// <param name="dtData">要导出的数据</param>
    /// <param name="dtColumn">列名称</param>
    /// <param name="excelTitle">导出后显示列名称</param>
    /// <param name="path"></param>
    /// <param name="fileName"></param>
    /// <param name="mustCoulums">主表列</param>
    /// <param name="dicDropList">下拉框集合</param>
    /// <param name="hideCoulums">隐藏列</param>

    public static void DataTableToExcel(DataTable dtData, string[] dtColumn, String[] excelTitle, string path, string fileName, string[] mustCoulums, Dictionary<int, DataTable> dicDropList = null, string[] hideCoulums = null)
    {
    IWorkbook hssfworkbook = null;
    ISheet sheet = null;
    string cellValue = "";

    if (fileName == "")
    {
    fileName = "代采付款记录" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; //默认名称和版本
    }
    fileName = path + "\" + fileName;
    if (Path.GetExtension(fileName) == ".xls")//2003 版本
    {
    hssfworkbook = new HSSFWorkbook();
    }
    else if (Path.GetExtension(fileName) == ".xlsx") //2007及以上版本
    {
    hssfworkbook = new XSSFWorkbook();
    }
    #region 此为主簿
    sheet = hssfworkbook.CreateSheet("Sheet1");
    //创建表头行
    IRow rowMainName = sheet.CreateRow(0);
    if (excelTitle.Length > 0)
    {
    for (int i = 0; i < excelTitle.Length; i++)
    {
    ICell cellDetailHead = rowMainName.CreateCell(i);
    if (mustCoulums.Contains<string>(excelTitle[i].ToString()))
    {
    //HSSFCellStyle fCellStyle = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
    cellDetailHead.CellStyle = gFunc.Getcellstyle(hssfworkbook, gIEnum.Stylexls.Title, "main");
    }
    else
    {
    cellDetailHead.CellStyle = gFunc.Getcellstyle(hssfworkbook, gIEnum.Stylexls.Title);
    }
    cellDetailHead.SetCellValue(excelTitle[i]);
    }
    }
    int rowIndex = 0;

    if (dicDropList != null)
    {
    foreach (var item in dicDropList)
    {
    CellRangeAddressList regions = new CellRangeAddressList(1, 65535, item.Key, item.Key);
    string[] temp = new string[item.Value.Rows.Count];
    int i = 0;
    foreach (DataRow dr in item.Value.Rows)
    {
    temp[i] = dr[0].ToString();//字段
    i++;
    }
    DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(temp);
    HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
    dataValidate.CreateErrorBox("输入不合法", "请输入下拉列表中的值。");
    sheet.AddValidationData(dataValidate);
    }
    }
    //创建明细行
    if (dtData != null)
    {
    for (int i = 0; i < dtData.Rows.Count; i++)
    {
    rowIndex = rowIndex + 1;
    IRow rowDetailItem = sheet.CreateRow(rowIndex);
    for (int j = 0; j < dtColumn.Length; j++)
    {

    ICell cellDetailItem = rowDetailItem.CreateCell(j);
    cellDetailItem.CellStyle = gFunc.Getcellstyle(hssfworkbook, gFunc.DataTypeOfTypeForStyle(dtData.Columns[dtColumn[j]].DataType, dtColumn[j]));
    cellValue = dtData.Rows[i][dtColumn[j]].ToString();
    cellDetailItem.SetCellValue(cellValue);
    }
    }
    }
    //自动适应大小
    for (int i = 0; i < excelTitle.Length; i++)
    {
    sheet.AutoSizeColumn(i);
    }

    #endregion
    //影藏列
    if (hideCoulums != null && hideCoulums.Length > 0)
    {
    foreach (string item in hideCoulums)
    {
    for (int j = 0; j < excelTitle.Length; j++)
    {
    if (item == excelTitle[j])
    {
    sheet.SetColumnHidden(j, true);
    }
    }
    }
    }

    //保存
    FileStream fi = new FileStream(fileName, FileMode.Create);
    hssfworkbook.Write(fi);
    fi.Dispose();
    }

  • 相关阅读:
    懂一点Python系列——快速入门
    DatagridView 控件列顺序与设置的不一样
    WinForm Column cannot be added because its CellType property is null.
    Rabbitmq消息服务器通讯异常: name must not be blank
    Redis 数据库
    redis 安装与使用
    windows 服务的安装与卸载之bat脚本命令
    以超级管理员方式运行bat文件
    CMD 下切换目录
    C#导出Excel后关闭进程EXCEL.EXE
  • 原文地址:https://www.cnblogs.com/zhusk/p/10524969.html
Copyright © 2011-2022 走看看