C# 寫入 Excel
.ddl下載:http://epplus.codeplex.com/
//開檔 |
FileStream fs = new FileStream( @"D:\目標檔案.xlsx" , FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); |
//載入Excel檔案 |
ExcelPackage ep = new ExcelPackage(fs); |
ExcelWorksheet sheet = ep.Workbook.Worksheets[ "Sheet1" ]; //取得Sheet1 |
//int startRowNumber = sheet.Dimension.Start.Row; //起始列編號,從1算起 |
//int endRowNumber = sheet.Dimension.End.Row; //結束列編號,從1算起 |
//bool isHeader = true ; |
//if (isHeader) //有包含標題 |
//{ |
// startRowNumber += 1; |
//} |
//寫入標題文字 |
sheet.Cells[1, 11].Value = "緯度" ; |
sheet.Cells[1, 12].Value = "經度" ; |
//for ( int i = startRowNumber; i <= endRowNumber; i++) |
//{ |
//讀值 |
// string cellValue = sheet.Cells[i, 1].Value.ToString(); |
|
//寫值 |
// sheet.Cells[i, 1].Value = cellValue+ "test" ; |
|
//} |
fs.Close(); |
//建立檔案 |
fs = new FileStream( @"D:\目標檔案.xlsx" , FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); |
ep.SaveAs(fs); //存檔 |
fs.Close(); |
//關閉資源 |
ep.Dispose(); |
ep = null ; |
標籤: c#
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁