就是源碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
namespace 對(duì)excel表的讀寫(xiě)
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//創(chuàng)建讀取文件流
using (FileStream fs = File.Open(@"d:\2.xls", FileMode.Open))
{
//創(chuàng)建一個(gè)工作簿
using (Workbook wk = new HSSFWorkbook(fs))
{
//遍歷每個(gè)工作表
for (int i = 0; i < wk.NumberOfSheets; i++)
{
//根據(jù)索引獲取每個(gè)工作表
using (Sheet sh = wk.GetSheetAt(i))
{
//輸出每個(gè)表名
Console.WriteLine("============{0}=============", sh.SheetName);
//遍歷每行
for (int j = 0; j <= sh.LastRowNum; j++)
{
Row cr = sh.GetRow(j);
//遍歷每行中的列
for (int k = 0; k < cr.LastCellNum; k++)
{
//Console.Write(cr.GetCell(k).ToString() + "\t");
tt.AppendText(cr.GetCell(k).ToString() + "\t");
}
tt.AppendText("/n");
Console.WriteLine();
}
}
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (Workbook wk=new HSSFWorkbook())
{
using (Sheet sh=wk.CreateSheet("轉(zhuǎn)轉(zhuǎn)轉(zhuǎn)"))
{
for (int i = 0; i < 3; i++)
{
Row rw = sh.CreateRow(i);
Cell ce = rw.CreateCell(0);
ce.SetCellValue("姓名" + i);
Cell ce1 = rw.CreateCell(1);
ce1.SetCellValue("性別" + i);
Cell ce2 = rw.CreateCell(2);
ce2.SetCellValue("年齡" + i);
}
using (FileStream fs = File.OpenWrite(@"d:\2.xls"))
{
wk.Write(fs);
}
}
}
MessageBox.Show("寫(xiě)入成功帽芽!");
}
}
}