ClosedXML 예제들 ..
var ws = workbook.Worksheet(1);
// Define a range with the data
var firstTableCell = ws.FirstCellUsed();
// Copy the table to another worksheet
var wsCopy = ws.CopyTo("Contacts Copy");
firstTableCell.Value = "test";
workbook.SaveAs("CopyingRanges.xlsx");
How to force Close
var wb = new XLWorkbook();
var sheet1 = wb.Worksheets.Add("Sheet1");
var sheet2 = wb.Worksheets.Add("Sheet2");
sheet1.Cell(1, 1).Value = "Value on Sheet1";
var range1 = sheet1.Range("A1");
sheet2.Cell(1, 1).Value = range1;
sheet2.Cell(1, 1).Value = "We're on Sheet2 now";
wb.SaveAs("Text.xlsx");
ws.Range(1, 1, 1, 7).AddToNamed("TestRange");
var nR = wb.Range("TestRange");
var wb = new XLWorkbook();
var ws = wb.AddWorksheet("Sheet1");
ws.Cell("A1").AddToNamed("Test");
ws.Cell("A2").AddToNamed("Test");
ws.Range("B1:B2").AddToNamed("Test");
여러 워크시트를 한곳에 복사하기
I could have sworn there was a workbook.AddWorksheet(IXLWorksheet worksheet) overload. Anyway you can use:
var excelFiles = new List<String>(); // Fill list with your files
var wbCombined = new XLWorkbook();
foreach (var file in excelFiles)
{
var wbSource = new XLWorkbook(file);
foreach (var wsSource in wbSource.Worksheets)
{
wsSource.CopyTo(wbCombined, wsSource.Name);
}
}
Closing workbook without saving
http://closedxml.codeplex.com/discussions/248002
originalWorkbook = new XLWorkbook(ofd.OpenFile())
ToTable() to return TableData
http://closedxml.codeplex.com/discussions/277516
/// Get all data from a sheet Excell file
///</summary>
public System.Data.DataTable Import(string sheetName)
{
var datatable = new DataTable();
// var dataset = new DataSet();
var workbook = new XLWorkbook(this.FilePathXls + this.FileNameXls);
var xlWorksheet = workbook.Worksheet(sheetName);
//IXLRange xlRangeRow = xlWorksheet.AsRange();
var range = xlWorksheet.Range(xlWorksheet.FirstCellUsed(), xlWorksheet.LastCellUsed());
// IXLCell rowCell = xlWorksheet.LastCellUsed();
int col = range.ColumnCount();
int row = range.RowCount();
// add columns hedars
datatable.Clear();
for (int i = 1; i <= col; i++)
{
IXLCell column = xlWorksheet.Cell(1, i);
datatable.Columns.Add(column.Value.ToString());
}
// add rows data
int firstHeadRow = 0;
foreach (var item in range.Rows())
{
if (firstHeadRow != 0)
{
var array = new object[col];
for (int y = 1; y <= col; y++)
{
array[y - 1] = item.Cell(y).Value;
}
datatable.Rows.Add(array);
}
firstHeadRow++;
}
return datatable;
}