简体   繁体   中英

How to use excel in MS visual C++

I want to make a windows form app. 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. I got only the UI done, I know some basics but I have no idea how to combine MS Visual C++ and Excel.

There's a lot of libraries listed in this answer: What is a simple and reliable C library for working with Excel files?

In addition "ExcelFormat Library" is basic, but it sounds like it will do everything you need. It's free and easy to use.

And Number Duck is a commercial library that I have created.

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. :) 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. Than add this two lines: using namespace ExcelLibrary::CompoundDocumentFormat; using namespace ExcelLibrary::SpreadSheet;

The aim of this project is provide a native .NET solution to create, read and modify
Excel files without using COM interop or OLEDB connection.

Currently .xls (BIFF8) format is implemented. In future .xlsx (Excel 2007) may also be supported.

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);
 }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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