【问题标题】:how to add dataset to worksheet using Excellibrary如何使用 Excellibrary 将数据集添加到工作表
【发布时间】:2012-07-11 19:43:35
【问题描述】:

我必须在单个文件中添加多个工作表..我找不到将数据集添加到工作表的任何方法,但我知道如何添加工作表

【问题讨论】:

  • 您想将数据集添加到您的 Excel 工作表中吗?
  • 查看我的更新答案我已添加样式代码。

标签: asp.net excellibrary


【解决方案1】:

试试这个。

using System;
using System.Data;
using System.IO;
using ExcelLibrary.SpreadSheet;

namespace ExcelLibrary
{
  /// <summary>
   /// Provides simple way to convert Excel workbook into DataSet
  /// </summary>
 public sealed class DataSetHelper
 {
    /// <summary>
    /// Populate all data (all converted into String) in all worksheets 
    /// from a given Excel workbook.
    /// </summary>
    /// <param name="filePath">File path of the Excel workbook</param>
    /// <returns>DataSet with all worksheet populate into DataTable</returns>
    public static DataSet CreateDataSet(String filePath)
    {
        DataSet ds = new DataSet();
        Workbook workbook = Workbook.Load(filePath);
        foreach (Worksheet ws in workbook.Worksheets)
        {
            DataTable dt = PopulateDataTable(ws);
            ds.Tables.Add(dt);
        }
        return ds;
    }

    /// <summary>
    /// Populate data (all converted into String) from a given Excel 
    /// workbook and also work sheet name into a new instance of DataTable.
    /// Returns null if given work sheet is not found.
    /// </summary>
    /// <param name="filePath">File path of the Excel workbook</param>
    /// <param name="sheetName">Worksheet name in workbook</param>
    /// <returns>DataTable with populate data</returns>
    public static DataTable CreateDataTable(String filePath, String sheetName)
    {
        Workbook workbook = Workbook.Load(filePath);
        foreach (Worksheet ws in workbook.Worksheets)
        {
            if (ws.Name.Equals(sheetName))
                return PopulateDataTable(ws);
        }
        return null;
    }

    private static DataTable PopulateDataTable(Worksheet ws)
    {
        CellCollection Cells = ws.Cells;

        // Creates DataTable from a Worksheet
        // All values will be treated as Strings
        DataTable dt = new DataTable(ws.Name);

        // Extract columns
        for (int i = 0; i <= Cells.LastColIndex; i++)
            dt.Columns.Add(Cells[0, i].StringValue, typeof(String));

        // Extract data
        for (int currentRowIndex = 1; currentRowIndex <= Cells.LastRowIndex; currentRowIndex++)
        {
            DataRow dr = dt.NewRow();
            for (int currentColumnIndex = 0; currentColumnIndex <= Cells.LastColIndex; currentColumnIndex++)
                dr[currentColumnIndex] = Cells[currentRowIndex, currentColumnIndex].StringValue;
            dt.Rows.Add(dr);
        }

        return dt;
    }

    /// <summary>
    /// Populate all data from the given DataSet into a new Excel workbook
    /// </summary>
    /// <param name="filePath">File path to new Excel workbook to be created</param>
    /// <param name="dataset">Source DataSet</param>
    public static void CreateWorkbook(String filePath, DataSet dataset)
    {
        if (dataset.Tables.Count == 0)
            throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset");

        Workbook workbook = new Workbook();
        foreach (DataTable dt in dataset.Tables)
        {
            Worksheet worksheet = new Worksheet(dt.TableName);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                // Add column header
                worksheet.Cells[0, i] = new Cell(dt.Columns[i].ColumnName);

                // Populate row data
                for (int j = 0; j < dt.Rows.Count; j++)
                    worksheet.Cells[j + 1, i] = new Cell(dt.Rows[j][i]);
            }
            workbook.Worksheets.Add(worksheet);
        }
        workbook.Save(filePath);
    }
}
}

来自code.google.com 对于样式和颜色

HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();

// cell background
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

// font color
 HSSFFont font1 = hssfworkbook.CreateFont();
 font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
 style1.SetFont(font1);

cell.CellStyle = style1;

更多详情请看这篇帖子How can I change cell style in an Excel file with ExcelLibrary?

【讨论】:

  • 你能帮忙给单元格背景颜色吗...我试过 worksheet.Cells[0, i].Style = new CellStyle() { BackColor = System.Drawing.Color.Red } ;但我没有工作..谢谢
  • 那么请将其标记为您的答案并投票,因为您的主要问题已解决。改变颜色我不知道,但我正在寻找它,当我找到我会告诉你的。
  • @gillchetanpalsingh 查看我的更新答案我添加了样式代码。
猜你喜欢
  • 2016-01-02
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
  • 2017-01-29
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
相关资源
最近更新 更多