
这里用到了NPOI中自带的ShiftRows函数,该函数功能:对指定行移动 *** 作
public void ShiftRows(int startRow, int endRow, int n)
public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight)
public void ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight, bool moveComments)
思路:
一:先确要从第几行插入----int InsertRowIndex
二:指定要插入多少行----int InsertRowCount
三:指定InsertRowIndex上面那行----HSSFRow mySourceStyleRow,并获取他的格式:
获取InsertRowIndex上面那行的方法:InsertRowIndex-1.
打开一个Workbook 可以用到函数NPOIOpenExcel(string fileName)
获取行:
HSSFSheet mySheet= Workbook.GetSheetAt(Workbook.ActiveSheetIndex)
HSSFRow mySourceStyleRow=mySheet.GetRow(InsertRowIndex-1)
四:对InsertRowIndex那一行开始到最后一行:HSSFSheet类下的属性.LastRowNum(获取最后有效的行数)都批量用ShiftRows函数往下移动InsertRowCount行,这时会在第InsertRowIndex行里,出现了InsertRowCount行空行,这里只要对空行的格式都设置为mySourceStyleRow行的格式的每一个相应的单元格格式即可
这里用到函数 MyInsertRow()即可
示例,假如我要在指定的表的第3行插入3行,可以使用这样写:
int InsertRowIndex=2//指定在第几行插入,我们这里测试用第3行,对应NPOI的索引值2,因为从0起
int InsertRowCount=3//要插入的行数
HSSFWorkbook Workbook=NPOIOpenExcel(@"E:\TEST\MyExcelTestFile.xls")//打开工作薄
HSSFSheet mySheet= Workbook.GetSheetAt(Workbook.ActiveSheetIndex)//获取工作表
HSSFRow mySourceStyleRow=mySheet.GetRow(InsertRowIndex-1)//获取源格式行
//调用插入行方法
MyInsertRow(mySheet,InsertRowIndex,InsertRowCount,mySourceStyleRow)
//参数说明
//第一个:指定 *** 作的Sheet。
//第二个:指定在第几行指入(插入行的位置)
//第三个:指定要插入多少行
//第四个:源单元格格式的行,
函数部分:
public HSSFWorkbook NPOIOpenExcel(string FileName)
{
HSSFWorkbook MyHSSFWorkBook
Stream MyExcelStream = OpenClasspathResource(FileName)
MyHSSFWorkBook = new HSSFWorkbook(MyExcelStream)
return MyHSSFWorkBook
}
private void MyInsertRow(HSSFSheet sheet, int 插入行, int 插入行总数, HSSFRow 源格式行)
{
#region 批量移动行
sheet
.ShiftRows
(
插入行, //--开始行
sheet
.LastRowNum,//--结束行
插入行总数, //--移动大小(行数)--往下移动
true, //是否复制行高
false, //是否重置行高
true//是否移动批注
)
#endregion
#region 对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源(即:插入行-1的那一行)
for (int i = 插入行i <插入行 + 插入行总数 - 1i++)
{
HSSFRow targetRow = null
HSSFCell sourceCell = null
HSSFCell targetCell = null
targetRow = sheet.CreateRow(i + 1)
for (int m = 源格式行.FirstCellNumm <源格式行.LastCellNumm++)
{
sourceCell = 源格式行.GetCell(m)
if (sourceCell == null)
continue
targetCell = targetRow.CreateCell(m)
targetCell.Encoding = sourceCell.Encoding
targetCell.CellStyle = sourceCell.CellStyle
targetCell.SetCellType(sourceCell.CellType)
}
//CopyRow(sourceRow, targetRow)
//Util.CopyRow(sheet, sourceRow, targetRow)
}
HSSFRow firstTargetRow = sheet.GetRow(插入行)
HSSFCell firstSourceCell = null
HSSFCell firstTargetCell = null
for (int m = 源格式行.FirstCellNumm <源格式行.LastCellNumm++)
{
firstSourceCell = 源格式行.GetCell(m)
if (firstSourceCell == null)
continue
firstTargetCell = firstTargetRow.CreateCell(m)
firstTargetCell.Encoding = firstSourceCell.Encoding
firstTargetCell.CellStyle = firstSourceCell.CellStyle
firstTargetCell.SetCellType(firstSourceCell.CellType)
}
#endregion
}
在C#中使用NPOI2.0 *** 作Excel2003,代码如下:
使用NPOI.HSSF.UserModel空间下的HSSFWorkbook *** 作Excel2003
#region Excel2003
/// <summary>
/// 将Excel文件中的数据读出到DataTable中(xls)
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static DataTable ExcelToTableForXLS(string file)
{
DataTable dt = new DataTable()
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
XLS.HSSFWorkbook hssfworkbook = new XLS.HSSFWorkbook(fs)
ISheet sheet = hssfworkbook.GetSheetAt(0)
//表头
IRow header = sheet.GetRow(sheet.FirstRowNum)
List<int>columns = new List<int>()
for (int i = 0i <header.LastCellNumi++)
{
object obj = GetValueTypeForXLS(header.GetCell(i) as XLS.HSSFCell)
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + i.ToString()))
//continue
}
else
dt.Columns.Add(new DataColumn(obj.ToString()))
columns.Add(i)
}
//数据
for (int i = sheet.FirstRowNum + 1i <= sheet.LastRowNumi++)
{
DataRow dr = dt.NewRow()
bool hasValue = false
foreach (int j in columns)
{
dr[j] = GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) as XLS.HSSFCell)
if (dr[j] != null &&dr[j].ToString() != string.Empty)
{
hasValue = true
}
}
if (hasValue)
{
dt.Rows.Add(dr)
}
}
}
return dt
}
/// <summary>
/// 将DataTable数据导出到Excel文件中(xls)
/// </summary>
/// <param name="dt"></param>
/// <param name="file"></param>
public static void TableToExcelForXLS(DataTable dt, string file)
{
XLS.HSSFWorkbook hssfworkbook = new XLS.HSSFWorkbook()
ISheet sheet = hssfworkbook.CreateSheet("Test")
//表头
IRow row = sheet.CreateRow(0)
for (int i = 0i <dt.Columns.Counti++)
{
ICell cell = row.CreateCell(i)
cell.SetCellValue(dt.Columns[i].ColumnName)
}
//数据
for (int i = 0i <dt.Rows.Counti++)
{
IRow row1 = sheet.CreateRow(i + 1)
for (int j = 0j <dt.Columns.Countj++)
{
ICell cell = row1.CreateCell(j)
cell.SetCellValue(dt.Rows[i][j].ToString())
}
}
//转为字节数组
MemoryStream stream = new MemoryStream()
hssfworkbook.Write(stream)
var buf = stream.ToArray()
//保存为Excel文件
using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
{
fs.Write(buf, 0, buf.Length)
fs.Flush()
}
}
/// <summary>
/// 获取单元格类型(xls)
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
private static object GetValueTypeForXLS(XLS.HSSFCell cell)
{
if (cell == null)
return null
switch (cell.CellType)
{
case CellType.BLANK: //BLANK:
return null
case CellType.BOOLEAN: //BOOLEAN:
return cell.BooleanCellValue
case CellType.NUMERIC: //NUMERIC:
return cell.NumericCellValue
case CellType.STRING: //STRING:
return cell.StringCellValue
case CellType.ERROR: //ERROR:
return cell.ErrorCellValue
case CellType.FORMULA: //FORMULA:
default:
return "=" + cell.CellFormula
}
}
#endregion
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)