1

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.

Ove
  • 6,227
  • 2
  • 39
  • 68
SurisDziugas
  • 160
  • 1
  • 3
  • 12
  • 3
    You've got two choices: use COM automation to get Excel itself to do the work, or find a library that will allow you to generate Excel files without Excel. N.B. you can't use the COM solution without an interactive desktop, and it does require that Excel be installed. Alas I don't know of any C++ libraries to do this but there are plenty for e.g. [C#](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp) or Java. – Rup May 24 '13 at 15:38
  • 1
    define "excel file". xls?xlsx?excelml?csv?html? – Sheng Jiang 蒋晟 May 24 '13 at 16:11

2 Answers2

3

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.

Community
  • 1
  • 1
1

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);
 }
 }
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • There must be better ways to do this than add a dependency on .NET? – Rup Jun 02 '14 at 11:19
  • Of course there's another way to solve the thing about Excel extensions but no one tried to help the man. I gave him a hint that something can be resolved because it is written that only knows some of the basics of programming. – Stanojkovic Jun 03 '14 at 13:19