我必须从数据表中将数据导出到 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数据看起来像
但我需要生成这种 excel 文件,其中数据和图表都将在同一张表中。这是屏幕截图。