簡體   English   中英

Excellibrary 文檔

[英]Excellibrary documentation

我在這里找到了許多使用ExcelLibrary編輯Excel文件的建議,但我在任何地方都找不到任何文檔。

http://code.google.com/p/excellibrary/

試試這個:

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

這不是最漂亮的代碼,但它返回一個DataTable 我建議您盡可能直接使用這些值,即。 而不是轉換為DataTable直接讀取值並跳過此轉換步驟。

這個問題及其答案真的很古老。 現在任何人都在看這個 - 忘記 ExcelLibrary。 NPOI 現在是要走的路,並且適用於 .xls 和 .xlsx

https://github.com/nissl-lab/npoi - 從哪里獲得 C# 下載
https://poi.apache.org/ - 我找到的最好的文檔,即使它是 Java 版本。

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

我不確定這是什么,但我認為操作 Microsoft Office 文檔的首選方法是使用Open XML SDK 2.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM