mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
organize and update samples
This commit is contained in:
parent
6b3781d03b
commit
4d1e5024b1
|
@ -1,37 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
for(auto sheet : xlnt::reader::load_workbook("book.xlsx"))
|
||||
{
|
||||
std::cout << sheet.get_title() << ": " << std::endl;
|
||||
|
||||
for(auto row : sheet.rows())
|
||||
{
|
||||
for(auto cell : row)
|
||||
{
|
||||
std::cout << cell.get_value().as<std::string>() << ", ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
xlnt::workbook workbook;
|
||||
|
||||
for(int i = 0; i < 3; i++)
|
||||
{
|
||||
auto sheet = workbook.create_sheet("Sheet" + std::to_string(i));
|
||||
|
||||
for(int row = 0; row < 100; row++)
|
||||
{
|
||||
for(int column = 0; column < 100; column++)
|
||||
{
|
||||
sheet[xlnt::cell_reference(column, row)].set_value(row * 100 + column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
workbook.save("book2.xlsx");
|
||||
}
|
37
samples/sample1.cpp
Normal file
37
samples/sample1.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
|
||||
/// <summary>
|
||||
/// Read sample1.xlsx and print out a 2-dimensional
|
||||
/// representation of each sheet. Cells are separated by commas.
|
||||
/// Each new line is a new row.
|
||||
/// </summary>
|
||||
int main()
|
||||
{
|
||||
// Create a new workbook by reading sample1.xlsx in the current directory.
|
||||
auto wb = xlnt::reader::load_workbook("sample1.xlsx");
|
||||
|
||||
// The workbook class has begin and end methods so it can be iterated upon.
|
||||
// Each item is a sheet in the workbook.
|
||||
for(auto sheet : wb)
|
||||
{
|
||||
// Print the title of the sheet on its own line.
|
||||
std::cout << sheet.get_title() << ": " << std::endl;
|
||||
|
||||
// Iterating on a range, such as from worksheet::rows, yields cell_vectors.
|
||||
// Cell vectors don't actually contain cells to reduce overhead.
|
||||
// Instead they hold a reference to a worksheet and the current cell_reference.
|
||||
// Internally, calling worksheet::get_cell with the current cell_reference yields the next cell.
|
||||
// This allows easy and fast iteration over a row (sometimes a column) in the worksheet.
|
||||
for(auto row : sheet.rows())
|
||||
{
|
||||
for(auto cell : row)
|
||||
{
|
||||
// cell::operator<< adds a string represenation of the cell's value to the stream.
|
||||
std::cout << cell << ", ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
BIN
samples/sample1.xlsx
Normal file
BIN
samples/sample1.xlsx
Normal file
Binary file not shown.
44
samples/sample2.cpp
Normal file
44
samples/sample2.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <iostream>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
|
||||
/// <summary>
|
||||
/// Make 3 sheets containing 10,000 cells with unique numeric values.
|
||||
/// </summary>
|
||||
int main()
|
||||
{
|
||||
// create the workbook
|
||||
xlnt::workbook workbook;
|
||||
|
||||
// workbooks have a single sheet called "Sheet" initially so let's get rid of it
|
||||
workbook.clear();
|
||||
|
||||
for(int i = 0; i < 3; i++)
|
||||
{
|
||||
// the title will be "Sample2-1", "Sample2-2", or "Sample2-3"
|
||||
// if we don't specify a title, these would be "Sheet#" where
|
||||
// # is the lowest number that doesn't have a corresponding sheet yet.
|
||||
auto sheet = workbook.create_sheet("Sample2-" + std::to_string(i));
|
||||
|
||||
for(int row = 1; row < 101; row++)
|
||||
{
|
||||
for(int column = 1; column < 101; column++)
|
||||
{
|
||||
// Since we can't overload subscript to accept both number,
|
||||
// create a cell_reference which "points" to the current cell.
|
||||
xlnt::cell_reference ref(column, row);
|
||||
|
||||
// This is important!
|
||||
// The cell class is really just a wrapper around a pointer.
|
||||
// For this reason, we can store them by value and still modify
|
||||
// the data in the containing worksheet.
|
||||
auto cell = sheet[ref];
|
||||
|
||||
// set_value has overloads for many types such as strings and ints
|
||||
cell.set_value(row * 100 + column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This will be written to the current directory.
|
||||
workbook.save("sample2.xlsx");
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
import sys
|
||||
|
||||
def format(args):
|
||||
print()
|
||||
for line in sys.stdin.readlines():
|
||||
print('"' + line.rstrip().replace('"', '\\"') + '"')
|
||||
|
||||
if __name__ == '__main__':
|
||||
format(sys.argv)
|
Loading…
Reference in New Issue
Block a user