如何在MS visual c++中使用excel

How to use excel in MS visual C++

本文关键字:excel c++ visual MS      更新时间:2023-10-16

我想做一个windows窗体应用程序。你可以在textBox'es中写入文本,当你按下按钮时,应用程序会创建一个excel文件,并从框中写入文本。我只完成了UI,我知道一些基础知识,但我不知道如何结合MS Visual c++和Excel。

这个答案中列出了很多库:什么是一个简单可靠的C库与Excel文件的工作?

此外,"ExcelFormat Library"是基本的,但听起来它会做你需要的一切。它是免费的,很容易使用。

And Number Duck是我创建的一个商业图书馆

这是一个c#代码取自https://code.google.com/p/excellibrary/,在vc++中使用这段代码并使其工作。:)语法不同,但如果你仔细想想,你会发现它们都是一样的。;)首先,您必须下载ExcelLibrary.dll文件并将其添加到参考项目中。然后加上这两行:使用命名空间ExcelLibrary::CompoundDocumentFormat

使用ExcelLibrary::SpreadSheet;

这个项目的目的是提供一个本地。net解决方案来创建、读取和修改
Excel文件,不使用COM互操作或OLEDB连接。

目前实现。xls (BIFF8)格式。将来。xlsx (Excel 2007)也可能是支持。

Example code: 
//create new xls file
string file = "C:\newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY-MM-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);
// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];
// traverse cells
foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
{
 dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
}
// traverse rows by Index
for (int rowIndex = sheet.Cells.FirstRowIndex; 
rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
{
 Row row = sheet.Cells.GetRow(rowIndex);
 for (int colIndex = row.FirstColIndex; 
 colIndex <= row.LastColIndex; colIndex++)
 {
 Cell cell = row.GetCell(colIndex);
 }
 }