使用简单的类创建 Excel 文件






4.85/5 (10投票s)
2007 年 3 月 9 日
4分钟阅读

305766

2853
创建可在 Excel 中打开的 XML 文件。
引言
当我编写最新的应用程序时,我需要找到一种方法来打印来自 CListCtrl 的数据。由于我对 MFC 中的打印不太熟悉,我想到了将数据导出到 Excel,并将所有格式设置和打印工作交给 Excel。
我的第一次尝试是创建一个包含所有必需数据的 CSV 文件。它工作得很好,但用户抱怨说他们必须自己进行格式设置。所以我尝试找出如何创建 XLS 文件。
在 Excel 2003 中,可以导入 XML 文件。因此,我创建了一个 XML 文件并对其进行了分析。结果是一个新的类,可以帮助从你的 MFC 应用程序生成 Excel 文件。
工作原理
Excel-XML 文件包含许多预定义的样式。定义的每个单元格都需要自己的样式。因此,当使用不同的格式选项时,您可能会得到大量的样式。这些样式必须在 XML 文件的开头定义。此外,您必须定义 XML 文件中使用的每一列。并且您需要确切地知道 XML 文件中有多少行数据。
因此,无论何时创建 XML 文件,都会在您的临时文件夹中创建一个文件。可以使用此 XML 文件打开 Excel,然后您可以删除临时 XML 文件。出于兼容性原因,我在我的类中保留了 CSV 文件的创建,以防您的系统上未安装 Excel,但又想导出数据。
步骤
第一步是将文件 *Excel.cpp* 和 *Excel.h* 添加到您的项目中。在您要创建 XML 文件的任何源文件中包含 *Excel.h*。
要开始导出,您必须声明一个 CExcel 类型的变量。构造函数也可以与文件名的一部分一起使用。所有临时文件都将以此字符串开头。您可以在您的临时文件夹中创建自己的文件。文件的开头默认为 XLS。
CExcel export("TST");
下一步是添加您想在文件中包含的所有列
export.AddColumn(120.3);
export.AddColumn();
对您需要的每个其他列使用 AddCloumn()
。您可以指定此列的宽度。如果您不指定值,Excel 将使用默认值。
下一步有点棘手。您必须定义要使用的样式。每个数据单元格都可以有自己的样式、字体大小和数据类型。AddStyle()
为您创建的每个样式返回一个唯一的标识符
int bold,center,bolddate,large;
bold=export.AddStyle(XLS_BOLD);
center=export.AddStyle(XLS_HCENTER|XLS_VCENTER);
bolddate=export.AddStyle(XLS_BOLD|XLS_DATE);
large=export.AddStyle(XLS_NOSTYLE,"16");
上面的示例创建了一个样式(存储在 bold
中),它只是将字体设置为粗体。Center
水平和垂直居中该值。bolddate
将保存用于显示粗体日期的样式的标识符。最后,large
将字体大小设置为 16 pt。
您可以在设置样式时使用这些标志的组合
XLS_NOSTYLE No change in style
XLS_HCENTER horizontaly centered
XLS_HLEFT left aligned
XLS_HRIGHT right aligned
XLS_VTOP aligned at the top of the cell
XLS_VCENTER vertically centered
XLS_VBOTTOM aligned at the bottom of the cell
XLS_WRAP text is wrapped to fit in the cell
XLS_BORDER there is a single line border around the cell
XLS_BOLD the font is bold
XLS_LINE the font is underlined with a single line
XLS_ITALICS the font is italics
XLS_DATE the cell contains a date
XLS_TIME the cell contains a time
XLS_CURRENCY the cell contains a currency
这些标志的每种组合都会产生一种新的样式。CExcel 将样式存储在 CStringArray 中,因此对样式没有限制。
在定义所有样式并添加所有列后,您可以开始使用以下命令创建 XML 文件:
export.Open(12,"Test");
Excel 需要在写入行之前知道行数,因此您现在必须指定此数字。您还必须为 Excel 工作表命名。
如果您使用不带参数的 Open()
,则不会创建 XML 文件。而是会创建一个 CSV 文件。
Open()
在您的文件中写入大量 XML 标头内容,并定义您添加的所有样式和列。
打开文件后,您可以添加数据行。每一行都必须以 NewRow()
开头。
export.NewRow(bold,25.3);
这开始了一个新行。该行采用粗体样式,高度为 25.3。如果您不指定任何值,则使用 Excel 默认值。如果您只是想设置新高度而不使用样式,请观察以下示例
export.NewRow(XLS_NOSTYLE,40.2);
在开始一行后,您可以将值添加到您行中的每个单元格
export.SetCell("This is for strings");
export.SetCell(129.3); // for float
export.SetCell(2); // for integer
您还可以指定样式
export.SetCell("this will be bold",bold);
export.SetCell(2,large); // a really big 2
export.SetCell(23.33,currency); // as far as you have defined currency-style
您还可以在 SetCell()
中使用 CTime
对象。但是,您必须指定一个将 CTime
定义为日期或时间的样式
int aTime=export.AddStyle(XLS_TIME|HCENTER);
CTime now;
export.SetCell(now,aTime); // a time horizontally centered
当您为单元格设置所有值后,您必须使用以下命令结束您的行:export.EndRow();
现在您可以开始下一行了。
当您完成所有数据后,您必须使用以下命令关闭您的文件:export.Close();
export.Run();
将使用您的 XML 文件打开 Excel。或者使用您的 CSV 文件,如果您在打开文件时没有指定参数。
显示后使用 export.Delete();
删除 CSV 文件。当 Excel 仍在运行时,不应删除 XML 文件,因此请使用
CExcel export;
export.DeleteAll();
改为在您的 OnClose()
函数中使用。
完整示例
这是一个完整的例子
void OnExport()
{
CExcel export("MYTMP"); // Create Files starting with MYTMP
int x_bold,x_center,x_date,x_money; // all the styles I want
CTime now=CTime::GetCurrentTime(); // this is now
export.AddColumn(); // first column
export.AddColumn();
export.AddColumn(123.3); // a wide column
x_bold=export.AddStyle(XLS_BOLD,"16"); // a bold-Font, 16 pt
x_center=export.AddStyle
(XLS_HCENTER|XLS_VCENTER|XLS_WRAP); // Centered text
x_date=export.AddStyle(XLS_DATE); // used to format a date
x_money=export.AddStyle(XLS_CURRENCY); // used for currencies
export.Open(3,"Example"); // Open the file with 3 rows
export.NewRow(x_bold,23); // a row of bold Text
export.SetCell("Date"); // some headlines
export.SetCell("Income");
export.SetCell("Why");
export.EndRow(); // End of first row
export.NewRow(); // an empty line
export.EndRow();
export.NewRow(); // next row
export.SetCell(now,x_date); // the CTime formatted as date
export.SetCell(72637.23,x_money); // a float value formatted as
// currency
export.SetCell("This is just long text,
which will be centered and wrapped around",x_center);
export.EndRow();
export.Close(); // closing the file
export.Run(); // starting Excel and display data
}
void OnClose()
{
CExcel export("MYTMP");
export.DeleteAll(); // Delete all export-files
CDialog::OnClose();
}
历史
2007 年 3 月 7 日首次发布