C# Excellibrary 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8928740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excellibrary documentation
提问by el ninho
I found many recommendations here to use ExcelLibraryfor editing excell files, but I can't find any documentation anywhere.
我在这里找到了许多使用ExcelLibrary编辑Excel文件的建议,但我在任何地方都找不到任何文档。
采纳答案by Lester
Try this:
尝试这个:
var workbook = Workbook.Load("spreadsheet.xls");
var worksheet = workbook.Worksheets[0]; // assuming only 1 worksheet
var cells = worksheet.Cells;
var dataTable = new DataTable("datatable");
// add columns
dataTable.Columns.Add("column1");
dataTable.Columns.Add("column2");
...
// add rows
for (int rowIndex = cells.FirstRowIndex + 1; rowIndex <= cells.LastRowIndex; rowIndex++)
{
var values = new List<string>();
foreach(var cell in cells.GetRow(rowIndex))
{
values.Add(cell.Value.StringValue);
}
dataTable.LoadDataRow(values.ToArray(), true);
}
It's not exactly the prettiest code but it returns a DataTable. I recommend that you just use the values directly if possible ie. instead of converting to a DataTableread the values directly and skip this conversion step.
这不是最漂亮的代码,但它返回一个DataTable. 我建议您尽可能直接使用这些值,即。而不是转换为DataTable直接读取值并跳过此转换步骤。
回答by BG100
I'm not sure what this is, but I think the preferred method of manipulating Microsoft Office documents is to use Open XML SDK 2.0.
我不确定这是什么,但我认为操作 Microsoft Office 文档的首选方法是使用Open XML SDK 2.0。
回答by SurfingSanta
This question and its answers are really old. Anyone looking at this now - forget ExcelLibrary. NPOI is now the way to go, and works well for both .xls and .xlsx
这个问题及其答案真的很古老。现在任何人都在看这个 - 忘记 ExcelLibrary。NPOI 现在是要走的路,并且适用于 .xls 和 .xlsx
https://npoi.codeplex.com/- where to get the C# downloads
https://poi.apache.org/- the best documentation I've found, even though it is the Java version.
https://npoi.codeplex.com/- 从哪里获得 C# 下载
https://poi.apache.org/- 我找到的最好的文档,即使它是 Java 版本。
回答by ladymbm
//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, 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);
}
}

