简体   繁体   English

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

[英]How to use excel in MS visual C++

I want to make a windows form app. 我想制作一个Windows窗体应用程序。 You can write text in textBox'es and when you press a button, the app would create an excel file and write the text from the boxes. 您可以在textBox中编写文本,当您按下按钮时,应用程序将创建一个Excel文件并从框中写入文本。 I got only the UI done, I know some basics but I have no idea how to combine MS Visual C++ and Excel. 我只完成了UI,我知道一些基础但我不知道如何结合MS Visual C ++和Excel。

There's a lot of libraries listed in this answer: What is a simple and reliable C library for working with Excel files? 这个答案中列出了很多库: 什么是用于处理Excel文件的简单可靠的C库?

In addition "ExcelFormat Library" is basic, but it sounds like it will do everything you need. 另外“ExcelFormat Library”是基本的,但听起来它会完成你需要的一切。 It's free and easy to use. 它免费且易于使用。

And Number Duck is a commercial library that I have created. Number Duck是我创建的商业图书馆。

This is a C # code taken from https://code.google.com/p/excellibrary/ , Play a bit with this code in VC + + and make it work. 这是一个来自https://code.google.com/p/excellibrary/的C#代码,在VC + +中使用此代码播放一下并使其正常工作。 :) Syntax are different but if you think about it you'll see it's all the same. :)语法不同但是如果你考虑一下,你会发现它们都是一样的。 ;) First you have to download ExcelLibrary.dll file and add it to the Reference Project. ;)首先,您必须下载ExcelLibrary.dll文件并将其添加到参考项目。 Than add this two lines: using namespace ExcelLibrary::CompoundDocumentFormat; 比添加这两行:using namespace ExcelLibrary :: CompoundDocumentFormat; using namespace ExcelLibrary::SpreadSheet; 使用命名空间ExcelLibrary :: SpreadSheet;

The aim of this project is provide a native .NET solution to create, read and modify 该项目的目的是提供一个用于创建,读取和修改的本机.NET解决方案
Excel files without using COM interop or OLEDB connection. 不使用COM互操作或OLEDB连接的Excel文件。

Currently .xls (BIFF8) format is implemented. 目前实现.xls(BIFF8)格式。 In future .xlsx (Excel 2007) may also be supported. 将来也可以支持.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);
 }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM