參考:http://no2don.blogspot.com/2013/02/c-nopi-excel-xls.html

下載NPOI的dll,放到專案 (共五個dll)

using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;

//建立 Workbook
XSSFWorkbook wb = new XSSFWorkbook();

//建立一個叫做"Sheet1"的 Sheet
XSSFSheet st = (XSSFSheet)wb.CreateSheet("Sheet1");

//生成一個新Row (第一列:0, 第二列:1, ...類推)
int row_index = 0;
st.CreateRow(row_index);
//生成這個Row的Cell, 且放值進去 (第一格:0, 第二格:1, ...類推)
st.GetRow(row_index).CreateCell(0, CellType.String).SetCellValue("內容值"); //A1
st.GetRow(row_index).CreateCell(1, CellType.String).SetCellValue("內容值"); //B1

//再生成一個新Row
st.CreateRow(++row_index);
//生成這個Row的Cell, 且放值進去
st.GetRow(row_index).CreateCell(0, CellType.String).SetCellValue("內容值"); //A2
st.GetRow(row_index).CreateCell(1, CellType.String).SetCellValue("內容值"); //B2

//寫入
var file = new FileStream("檔案路徑\\檔案名稱.xlsx", FileMode.Create);
wb.Write(file);
file.Close();

//關閉 Workbook
wb = null;

arrow
arrow
    文章標籤
    C#
    全站熱搜

    伊 發表在 痞客邦 留言(0) 人氣()