很多例子在利用NPOI向Excel写数据时,都是创建一个新的文件,或者通过模版创建新文件,然后再写数据,输出新的文件。
现在就只需要在已有的Excel中插入几条数据,该如何实现?
------解决方案--------------------
问题出在你的修改没有保存会Excel。
OpenClasspathResource和WriteToFile函数补上了。下面是我的测试代码(官网下的NPIO 2.0版本):
using System;
using System.Windows.Forms;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
namespace NPOISample
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : Form
{
#region MainForm.Designer
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "插入行";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(128, 46);
this.Controls.Add(this.button1);
this.Name = "MainForm";
this.Text = "NPOISample";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button button1;
#endregion
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private string DataDir = AppDomain.CurrentDomain.BaseDirectory;
void Button1Click(object sender, EventArgs e)
{
button1.Enabled = false;
int InsertRowIndex=2;//指定在第几行插入,我们这里测试用第3行,对应NPOI的索引值2,因为从0起
int InsertRowCount=3;//要插入的行数
IWorkbook Workbook=NPOIOpenExcel(DataDir + @"MyExcel.xls");//打开工作薄
ISheet mySheet= Workbook.GetSheetAt(Workbook.ActiveSheetIndex);//获取工作表
IRow mySourceStyleRow=mySheet.GetRow(InsertRowIndex-1);//获取源格式行
//调用插入行方法
MyInsertRow(mySheet, InsertRowIndex, InsertRowCount, mySourceStyleRow);
WriteToFile(Workbook, DataDir + @"MyExcel.xls");
button1.Enabled = true;
}
public IWorkbook NPOIOpenExcel(string FileName)
{
IWorkbook MyWorkBook;
Stream MyExcelStream = OpenClasspathResource(FileName);
MyWorkBook = new HSSFWorkbook(MyExcelStream);
return MyWorkBook;
}
/**
* Opens a test sample file from the 'data' sub-package of this class's package.
* @return <c>null</c> if the sample file is1 not deployed on the classpath.
*/
private Stream OpenClasspathResource(String fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
return file;
}
private void WriteToFile(IWorkbook workbook, String fileName)
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Write);
workbook.Write(file);
file.Close();
}
//参数说明
//第一个:指定操作的Sheet。
//第二个:指定在第几行指入(插入行的位置)
//第三个:指定要插入多少行
//第四个:源单元格格式的行,