gpt4 book ai didi

c# - 如何使用 NPOI 库 c# 在 Excel 工作表中生成折线图和数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:41 24 4
gpt4 key购买 nike

我必须将数据从数据表导出到 Excel 工作表,而且我还需要在同一张工作表中显示折线图。我的问题是如何在同一张表中显示数据和图表。

我正在使用 NPOI 库版本 1.2.5.0 和 dotnet 2.0 我已经设法使用 NPOI 库以这种方式将数据从数据表导出到 excel。这是我的示例代码。

class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("First Name", typeof(string));
dt.Columns.Add("Last Name", typeof(string));
dt.Columns.Add("Salary", typeof(double));

DataRow dr = null;
dr = dt.NewRow();
dr[0] = "Konna";
dr[1] = "Lombard";
dr[2] = "3000";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "Tunip";
dr[1] = "Mansar";
dr[2] = "4000";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = "Dobby";
dr[1] = "Bhell";
dr[2] = "5000";
dt.Rows.Add(dr);

Utility.Export(dt, "Result");

}
}

public static class Utility
{
public static string GetParentDirectory()
{
System.IO.DirectoryInfo myDirectory = new DirectoryInfo(Environment.CurrentDirectory);
return myDirectory.Parent.Parent.FullName;
}

public static void Export( DataTable dt,string strSheetName)
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet(strSheetName);
HSSFRow headerRow = (HSSFRow) sheet.CreateRow(0);

IFont font = workbook.CreateFont();
font.FontHeightInPoints = 14;
font.FontName = "Calibri";
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;

ICell titleCell = headerRow.CreateCell(0);
titleCell.SetCellValue("Daily Finished Job History " + DateTime.Now.ToString("dd/MM/yyyy"));
titleCell.CellStyle = workbook.CreateCellStyle();
titleCell.CellStyle.SetFont(font);
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count));

int rowIndex = 2;
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
foreach (DataColumn column in dt.Columns)
{
font = workbook.CreateFont();
font.FontHeightInPoints = 11;
font.FontName = "Calibri";
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;

titleCell = dataRow.CreateCell(column.Ordinal);
titleCell.SetCellValue(column.ColumnName);
titleCell.CellStyle = workbook.CreateCellStyle();
titleCell.CellStyle.SetFont(font);

sheet.AutoSizeColumn(column.Ordinal);
}
rowIndex = 3;

foreach (DataRow row in dt.Rows)
{
dataRow = (HSSFRow)sheet.CreateRow(rowIndex);

foreach (DataColumn column in dt.Columns)
{
font = workbook.CreateFont();
font.FontHeightInPoints = 11;
font.FontName = "Calibri";
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.NORMAL;

titleCell = dataRow.CreateCell(column.Ordinal);
titleCell.SetCellValue(row[column].ToString());
titleCell.CellStyle = workbook.CreateCellStyle();
titleCell.CellStyle.SetFont(font);

sheet.AutoSizeColumn(column.Ordinal);
}

rowIndex++;
}



string strParentDirectory = GetParentDirectory();
strParentDirectory = strParentDirectory + "\\Data";
if (!Directory.Exists(strParentDirectory))
{
Directory.CreateDirectory(strParentDirectory );
}
string strFileName = strParentDirectory + "\\DailyFinishedJobHistory_" + DateTime.Now.ToString("yyyyMMdd")+".xls";
if (File.Exists(strFileName))
{
File.Delete(strFileName);
}
FileStream file = new FileStream(strFileName, FileMode.Create);
workbook.Write(file);

file.Close();
headerRow = null;
sheet = null;
workbook = null;
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
finally
{
dt.Dispose();
}
}
}

现在我的 excel 数据看起来像 enter image description here

但我需要生成这种 excel 文件,其中数据和图表都将在同一张表中。这是屏幕截图。

enter image description here

最佳答案

NPOI 2.1 将支持折线图。将于2014年6月发布

关于c# - 如何使用 NPOI 库 c# 在 Excel 工作表中生成折线图和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com