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();
    }

  • 相关阅读:
    ajax 拼接html标签 thinkphp
    使用Log4J进行日志操作
    学习Spark2.0中的Structured Streaming(一)
    互联网日志实时收集和实时计算的简单方案
    Integer.valueOf方法的源码解读
    spark的ML和MLLib两个包区别和联系?
    数组和集合区别
    Java中的集合类
    Spark会把数据都载入到内存么?
    可变参数
  • 原文地址:https://www.cnblogs.com/zhusk/p/10524969.html
Copyright © 2011-2022 走看看