NOPI read and write

Excel complete code for reading and writing
the using NPOI.HSSF.UserModel;
the using NPOI.SS.UserModel;
the using NPOI.XSSF.UserModel;
the using the System;
the using the System.IO;

ConsoleTest namespace
{
class Program
{
static void Main (String [] args)
{
// ReadFromExcelFile (@ "H: \ class file (15 software) \ 15 software engineering class list .xls");
WriteToExcel (@ "H: \ class files (15 software) \ 15 software engineering class list 1.xls ");
Console.ReadKey ();
}
public static void ReadFromExcelFile (String filePath)
{
IWorkbook wk = null;
String Extension = System.IO.Path.GetExtension (filePath);
the try
{
the using (FS = File.OpenRead the FileStream (filePath))
{
IF (extension.Equals (. "xls"))
{
// xls file data is written in wk
wk = WorkbookFactory.Create ( FS); // new new HSSFWorkbook (FS);
}
the else
{
// the data xlsx file written in wk
WorkbookFactory.Create = wk (FS); // new new XSSFWorkbook (FS);
}
}
// reading the current data table
ISheet Sheet wk.GetSheetAt = (0);
the IRow sheet.GetRow Row = (0); // read current line data
int offset = 0;
int = lastRowNum sheet.LastRowNum; // LastRowNum the current total number of rows in table 1 (note)
for (int I = 0; I <= lastRowNum; I ++)
{
row = sheet.GetRow ( i); // read the current line data
IF (row = null)!
{
int = lastCellNum row.LastCellNum;
// lastCellNum is the total number of columns in the current row
for (int J = 0; J <lastCellNum; J ++)
{
/ / j-th column reading the data row
String value = row.GetCell (j) .ToString ();
Console.Write (value.toString () + "");
}
Console.WriteLine ( "\ n-");
}
}
}
catch (Exception e)
{
//只在Debug模式下才输出
Console.WriteLine(e.Message);
}
}
public static void WriteToExcel(string filePath)
{
using (Stream fileStream = File.OpenWrite(filePath))
{
IWorkbook wb = new XSSFWorkbook();//如果生成xls则是HSSFWorkbook
ISheet sheet = wb.CreateSheet();
IRow row = sheet.CreateRow(0);//0行号
row.CreateCell(0).SetCellValue("rupeng");
row.CreateCell(1).SetCellValue(3.14);
wb.Write(fileStream);
}

}
}
}

Guess you like

Origin www.cnblogs.com/Lee-wlog/p/11391277.html