我必須將數據從數據表格導出到excel表格,並且還需要在同一張表格中顯示折線圖。我的問題是我怎麼能顯示數據&圖表在同一張表。如何使用NPOI庫生成折線圖以及excel表格中的數據使用NPOI庫c#
我使用NPOI庫版本1.2.5.0 & DOTNET 2.0我有管理導出數據使用NPOI圖書館這樣的方式,從數據表中出類拔萃。這裏是我的示例代碼。
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文件,其中數據&圖表都將在那裏在同一張紙上。這裏是屏幕截圖。