NPOI操作Excel辅助类

时间:2022-09-24 14:10:50
/// <summary>
/// NPOI操作excel辅助类
/// </summary>
public static class NPOIHelper
{
#region 定义与初始化
public static HSSFWorkbook workbook;
[Flags]
public enum LinkType
{
网址,
档案,
邮件,
内容
}; private static void InitializeWorkbook()
{
if (workbook == null)
workbook = new HSSFWorkbook();
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "測試公司";
workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "測試公司Excel檔案";
si.Title = "測試公司Excel檔案";
si.Author = "killysss";
si.Comments = "謝謝您的使用!";
workbook.SummaryInformation = si;
}
#endregion #region 资料形态转换
public static void WriteSteamToFile(MemoryStream ms, string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
byte[] data = ms.ToArray(); fs.Write(data, , data.Length);
fs.Flush();
fs.Close(); data = null;
ms = null;
fs = null;
}
public static void WriteSteamToFile(byte[] data, string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
fs.Write(data, , data.Length);
fs.Flush();
fs.Close();
data = null;
fs = null;
}
public static Stream WorkBookToStream(HSSFWorkbook InputWorkBook)
{
MemoryStream ms = new MemoryStream();
InputWorkBook.Write(ms);
ms.Flush();
ms.Position = ;
return ms;
}
public static HSSFWorkbook StreamToWorkBook(Stream InputStream)
{
HSSFWorkbook WorkBook = new HSSFWorkbook(InputStream);
return WorkBook;
}
public static HSSFWorkbook MemoryStreamToWorkBook(MemoryStream InputStream)
{
HSSFWorkbook WorkBook = new HSSFWorkbook(InputStream as Stream);
return WorkBook;
}
public static MemoryStream WorkBookToMemoryStream(HSSFWorkbook InputStream)
{
//Write the stream data of workbook to the root directory
MemoryStream file = new MemoryStream();
InputStream.Write(file);
return file;
}
public static Stream FileToStream(string FileName)
{
FileInfo fi = new FileInfo(FileName);
if (fi.Exists == true)
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
return fs;
}
else return null;
}
public static Stream MemoryStreamToStream(MemoryStream ms)
{
return ms as Stream;
}
#endregion #region DataTable与Excel资料格式转换
/// <summary>
/// 将DataTable转成Stream输出.
/// </summary>
/// <param name="SourceTable">The source table.</param>
/// <returns></returns>
public static Stream RenderDataTableToExcel(DataTable SourceTable)
{
HSSFWorkbook workbook = new HSSFWorkbook();
InitializeWorkbook(); MemoryStream ms = new MemoryStream();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); // 处理标题
foreach (DataColumn column in SourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // 处理内容
int rowIndex = ; foreach (DataRow row in SourceTable.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in SourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
} rowIndex++;
} workbook.Write(ms);
ms.Flush();
ms.Position = ; sheet = null;
headerRow = null;
workbook = null; return ms;
} /// <summary>
/// 将DataTable转成Workbook(自定资料型态)输出.
/// </summary>
/// <param name="SourceTable">The source table.</param>
/// <returns></returns>
public static HSSFWorkbook RenderDataTableToWorkBook(DataTable SourceTable)
{
HSSFWorkbook workbook = new HSSFWorkbook();
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); // handling header.
foreach (DataColumn column in SourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); // handling value.
int rowIndex = ; foreach (DataRow row in SourceTable.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in SourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
} rowIndex++;
}
return workbook;
} /// <summary>
/// 将DataTable资料输出成档案。
/// </summary>
/// <param name="SourceTable">The source table.</param>
/// <param name="FileName">文件保存路径</param>
public static void RenderDataTableToExcel(DataTable SourceTable, string FileName)
{
MemoryStream ms = RenderDataTableToExcel(SourceTable) as MemoryStream;
WriteSteamToFile(ms, FileName);
} /// <summary>
/// 从流读取资料到DataTable.
/// </summary>
/// <param name="ExcelFileStream">The excel file stream.</param>
/// <param name="SheetName">Name of the sheet.</param>
/// <param name="HeaderRowIndex">Index of the header row.</param>
/// <param name="HaveHeader">if set to <c>true</c> [have header].</param>
/// <returns></returns>
public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, bool HaveHeader)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook();
ISheet sheet = workbook.GetSheet(SheetName); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
string ColumnName = (HaveHeader == true) ? headerRow.GetCell(i).StringCellValue : "f" + i.ToString();
DataColumn column = new DataColumn(ColumnName);
table.Columns.Add(column);
} int rowCount = sheet.LastRowNum;
int RowStart = (HaveHeader == true) ? sheet.FirstRowNum + : sheet.FirstRowNum;
for (int i = RowStart; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++)
dataRow[j] = row.GetCell(j).ToString();
} ExcelFileStream.Close();
workbook = null;
sheet = null;
return table;
} /// <summary>
/// 從位元流讀取資料到DataTable.
/// </summary>
/// <param name="ExcelFileStream">The excel file stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="HeaderRowIndex">Index of the header row.</param>
/// <param name="HaveHeader">if set to <c>true</c> [have header].</param>
/// <returns></returns>
public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, bool HaveHeader)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook();
ISheet sheet = workbook.GetSheetAt(SheetIndex); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
string ColumnName = (HaveHeader == true) ? headerRow.GetCell(i).StringCellValue : "f" + i.ToString();
DataColumn column = new DataColumn(ColumnName);
table.Columns.Add(column);
} int rowCount = sheet.LastRowNum;
int RowStart = (HaveHeader == true) ? sheet.FirstRowNum + : sheet.FirstRowNum;
for (int i = RowStart; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
dataRow[j] = row.GetCell(j).ToString();
} table.Rows.Add(dataRow);
} ExcelFileStream.Close();
workbook = null;
sheet = null;
return table;
}
#endregion #region List<T>与Excel资料格式转换
public static Stream RenderListToExcel<T>(List<T> SourceList)
{
workbook = new HSSFWorkbook();
InitializeWorkbook(); MemoryStream ms = new MemoryStream();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); PropertyInfo[] properties = typeof(T).GetProperties(); int columIndex = ;
foreach (PropertyInfo column in properties)
{
headerRow.CreateCell(columIndex).SetCellValue(column.Name);
columIndex++;
} int rowIndex = ;
foreach (T item in SourceList)
{
IRow dataRow = sheet.CreateRow(rowIndex);
columIndex = ; foreach (PropertyInfo column in properties)
{
dataRow.CreateCell(columIndex).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString());
columIndex++;
}
rowIndex++;
} workbook.Write(ms);
ms.Flush();
ms.Position = ; sheet = null;
headerRow = null;
workbook = null; return ms;
} public static Stream RenderListToExcel<T>(List<T> SourceList, Dictionary<string, string> head)
{
workbook = new HSSFWorkbook();
InitializeWorkbook(); MemoryStream ms = new MemoryStream();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); PropertyInfo[] properties = typeof(T).GetProperties(); int columIndex = ;
foreach (PropertyInfo column in properties)
{
headerRow.CreateCell(columIndex).SetCellValue(head[column.Name] == null ? column.Name : head[column.Name].ToString());
columIndex++;
} int rowIndex = ;
foreach (T item in SourceList)
{
IRow dataRow = sheet.CreateRow(rowIndex);
columIndex = ; foreach (PropertyInfo column in properties)
{
dataRow.CreateCell(columIndex).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString());
columIndex++;
}
rowIndex++;
} workbook.Write(ms);
ms.Flush();
ms.Position = ; sheet = null;
headerRow = null;
workbook = null; return ms;
} public static HSSFWorkbook RenderListToWorkbook<T>(List<T> SourceList)
{
workbook = new HSSFWorkbook();
InitializeWorkbook(); ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); PropertyInfo[] properties = typeof(T).GetProperties(); int columIndex = ;
foreach (PropertyInfo column in properties)
{
headerRow.CreateCell(columIndex).SetCellValue(column.Name);
columIndex++;
} int rowIndex = ;
foreach (T item in SourceList)
{
IRow dataRow = sheet.CreateRow(rowIndex);
columIndex = ; foreach (PropertyInfo column in properties)
{
dataRow.CreateCell(columIndex).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString());
columIndex++;
}
rowIndex++;
} return workbook;
} public static HSSFWorkbook RenderListToWorkbook<T>(List<T> SourceList, Dictionary<string, string> head)
{
workbook = new HSSFWorkbook();
InitializeWorkbook(); ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(); PropertyInfo[] properties = typeof(T).GetProperties(); int columIndex = ;
foreach (PropertyInfo column in properties)
{
headerRow.CreateCell(columIndex).SetCellValue(head[column.Name] == null ? column.Name : head[column.Name].ToString());
columIndex++;
} int rowIndex = ;
foreach (T item in SourceList)
{
IRow dataRow = sheet.CreateRow(rowIndex);
columIndex = ; foreach (PropertyInfo column in properties)
{
dataRow.CreateCell(columIndex).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString());
columIndex++;
}
rowIndex++;
} return workbook;
} public static void RenderListToExcel<T>(List<T> SourceList, string FileName)
{
MemoryStream ms = RenderListToExcel(SourceList) as MemoryStream;
WriteSteamToFile(ms, FileName);
} public static void RenderListToExcel<T>(List<T> SourceList, Dictionary<string, string> head, string FileName)
{
MemoryStream ms = RenderListToExcel(SourceList, head) as MemoryStream;
WriteSteamToFile(ms, FileName);
} public static List<T> RenderListFromExcel<T>(Stream ExcelFileStream, string SheetName)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook(); ISheet sheet = workbook.GetSheet(SheetName);
IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; List<T> list = new List<T>(); for (int i = sheet.FirstRowNum + ; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
T t = Activator.CreateInstance<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
foreach (PropertyInfo column in properties)
{
int j = headerRow.Cells.FindIndex(delegate(ICell c)
{
return c.StringCellValue == column.Name;
}); if (j >= && row.GetCell(j) != null)
{
object value = ToType(column.PropertyType, row.GetCell(j).ToString());
column.SetValue(t, value, null);
}
} list.Add(t);
}
return list;
} public static List<T> RenderListFromExcel<T>(Stream ExcelFileStream, int SheetIndex)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook(); ISheet sheet = workbook.GetSheetAt(SheetIndex);
IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; List<T> list = new List<T>(); for (int i = sheet.FirstRowNum + ; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
T t = Activator.CreateInstance<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
foreach (PropertyInfo column in properties)
{
int j = headerRow.Cells.FindIndex(delegate(ICell c)
{
return c.StringCellValue == column.Name;
}); if (j >= && row.GetCell(j) != null)
{
object value = ToType(column.PropertyType, row.GetCell(j).ToString());
column.SetValue(t, value, null);
}
} list.Add(t);
}
return list;
} public static List<T> RenderListFromExcel<T>(Stream ExcelFileStream, string SheetName, Dictionary<string, string> head)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook(); ISheet sheet = workbook.GetSheet(SheetName);
IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; List<T> list = new List<T>(); for (int i = sheet.FirstRowNum + ; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
T t = Activator.CreateInstance<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
foreach (PropertyInfo column in properties)
{
int j = headerRow.Cells.FindIndex(delegate(ICell c)
{
return c.StringCellValue == (head[column.Name] == null ? column.Name : head[column.Name].ToString());
}); if (j >= && row.GetCell(j) != null)
{
object value = ToType(column.PropertyType, row.GetCell(j).ToString());
column.SetValue(t, value, null);
}
} list.Add(t);
}
return list;
} public static List<T> RenderListFromExcel<T>(Stream ExcelFileStream, int SheetIndex, Dictionary<string, string> head)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook(); ISheet sheet = workbook.GetSheetAt(SheetIndex);
IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
int cellCount = headerRow.LastCellNum; List<T> list = new List<T>(); for (int i = sheet.FirstRowNum + ; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
T t = Activator.CreateInstance<T>();
PropertyInfo[] properties = t.GetType().GetProperties();
foreach (PropertyInfo column in properties)
{
int j = headerRow.Cells.FindIndex(delegate(ICell c)
{
return c.StringCellValue == (head[column.Name] == null ? column.Name : head[column.Name].ToString());
}); if (j >= && row.GetCell(j) != null)
{
object value = ToType(column.PropertyType, row.GetCell(j).ToString());
column.SetValue(t, value, null);
}
} list.Add(t);
}
return list;
} public static object ToType(Type type, string value)
{
if (type == typeof(string))
{
return value;
} MethodInfo parseMethod = null; foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static
| BindingFlags.Public))
{
if (mi.Name == "Parse" && mi.GetParameters().Length == )
{
parseMethod = mi;
break;
}
} if (parseMethod == null)
{
throw new ArgumentException(string.Format(
"Type: {0} has not Parse static method!", type));
} return parseMethod.Invoke(null, new object[] { value });
}
#endregion #region 字符串阵列与Excel资料格式转换
/// <summary>
/// 建立datatable
/// </summary>
/// <param name="ColumnName">欄位名用逗號分隔</param>
/// <param name="value">data陣列 , rowmajor</param>
/// <returns>DataTable</returns>
public static DataTable CreateDataTable(string ColumnName, string[,] value)
{
/* 輸入範例
string cname = " name , sex ";
string[,] aaz = new string[4, 2];
for (int q = 0; q < 4; q++)
for (int r = 0; r < 2; r++)
aaz[q, r] = "1";
dataGridView1.DataSource = NewMediaTest1.Model.Utility.DataSetUtil.CreateDataTable(cname, aaz);
*/
int i, j;
DataTable ResultTable = new DataTable();
string[] sep = new string[] { "," }; string[] TempColName = ColumnName.Split(sep, StringSplitOptions.RemoveEmptyEntries);
DataColumn[] CName = new DataColumn[TempColName.Length];
for (i = ; i < TempColName.Length; i++)
{
DataColumn c1 = new DataColumn(TempColName[i].ToString().Trim(), typeof(object));
ResultTable.Columns.Add(c1);
}
if (value != null)
{
for (i = ; i < value.GetLength(); i++)
{
DataRow newrow = ResultTable.NewRow();
for (j = ; j < TempColName.Length; j++)
{
newrow[j] = string.Copy(value[i, j].ToString()); }
ResultTable.Rows.Add(newrow);
}
}
return ResultTable;
}
/// <summary>
/// Creates the string array.
/// </summary>
/// <param name="dt">The dt.</param>
/// <returns></returns>
public static string[,] CreateStringArray(DataTable dt)
{
int ColumnNum = dt.Columns.Count;
int RowNum = dt.Rows.Count;
string[,] result = new string[RowNum, ColumnNum];
for (int i = ; i < dt.Rows.Count; i++)
{
for (int j = ; j < dt.Columns.Count; j++)
{
result[i, j] = string.Copy(dt.Rows[i][j].ToString());
}
}
return result;
}
/// <summary>
/// 將陣列輸出成位元流.
/// </summary>
/// <param name="ColumnName">Name of the column.</param>
/// <param name="SourceTable">The source table.</param>
/// <returns></returns>
public static Stream RenderArrayToExcel(string ColumnName, string[,] SourceTable)
{
DataTable dt = CreateDataTable(ColumnName, SourceTable);
return RenderDataTableToExcel(dt);
}
/// <summary>
/// 將陣列輸出成檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="ColumnName">Name of the column.</param>
/// <param name="SourceTable">The source table.</param>
public static void RenderArrayToExcel(string FileName, string ColumnName, string[,] SourceTable)
{
DataTable dt = CreateDataTable(ColumnName, SourceTable);
RenderDataTableToExcel(dt, FileName);
}
/// <summary>
/// 將陣列輸出成WorkBook(自訂資料型態).
/// </summary>
/// <param name="ColumnName">Name of the column.</param>
/// <param name="SourceTable">The source table.</param>
/// <returns></returns>
public static HSSFWorkbook RenderArrayToWorkBook(string ColumnName, string[,] SourceTable)
{
DataTable dt = CreateDataTable(ColumnName, SourceTable);
return RenderDataTableToWorkBook(dt);
} /// <summary>
/// 將位元流資料輸出成陣列.
/// </summary>
/// <param name="ExcelFileStream">The excel file stream.</param>
/// <param name="SheetName">Name of the sheet.</param>
/// <param name="HeaderRowIndex">Index of the header row.</param>
/// <param name="HaveHeader">if set to <c>true</c> [have header].</param>
/// <returns></returns>
public static string[,] RenderArrayFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex, bool HaveHeader)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook();
ISheet sheet = workbook.GetSheet(SheetName); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(HeaderRowIndex);
int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
table.Columns.Add(column);
} int rowCount = sheet.LastRowNum;
int RowStart = (HaveHeader == true) ? sheet.FirstRowNum + : sheet.FirstRowNum;
for (int i = RowStart; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++)
dataRow[j] = row.GetCell(j).ToString();
} ExcelFileStream.Close();
workbook = null;
sheet = null;
return CreateStringArray(table);
} /// <summary>
/// 將位元流資料輸出成陣列.
/// </summary>
/// <param name="ExcelFileStream">The excel file stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="HeaderRowIndex">Index of the header row.</param>
/// <param name="HaveHeader">if set to <c>true</c> [have header].</param>
/// <returns></returns>
public static string[,] RenderArrayFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex, bool HaveHeader)
{
workbook = new HSSFWorkbook(ExcelFileStream);
InitializeWorkbook();
ISheet sheet = workbook.GetSheetAt(SheetIndex); DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(HeaderRowIndex);
int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
table.Columns.Add(column);
} int rowCount = sheet.LastRowNum;
int RowStart = (HaveHeader == true) ? sheet.FirstRowNum + : sheet.FirstRowNum;
for (int i = RowStart; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
dataRow[j] = row.GetCell(j).ToString();
} table.Rows.Add(dataRow);
} ExcelFileStream.Close();
workbook = null;
sheet = null;
return CreateStringArray(table);
}
#endregion #region 超链接
/// <summary>
/// 在位元流儲存格中建立超連結.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetNameOrIndex">Index of the sheet name or.</param>
/// <param name="LinkName">Name of the link.</param>
/// <param name="LinkValueOrIndex">Index of the link value or.</param>
/// <param name="s1">The s1.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="CellIndex">Index of the cell.</param>
/// <returns></returns>
public static Stream MakeLink(Stream InputStream, string SheetNameOrIndex, string LinkName, string LinkValueOrIndex, LinkType s1, int RowIndex, int CellIndex)
{
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ICellStyle hlink_style = workbook.CreateCellStyle();
IFont hlink_font = workbook.CreateFont();
hlink_font.Underline = FontUnderlineType.Single;
hlink_font.Color = IndexedColors.Blue.Index;
hlink_style.SetFont(hlink_font);
string ResultLinkValue = string.Empty;
int ResultSheet;
ISheet sheet;
if (int.TryParse(SheetNameOrIndex, out ResultSheet) == true)
sheet = workbook.GetSheetAt(ResultSheet);
else
sheet = workbook.GetSheet(SheetNameOrIndex);
ICell cell = sheet.CreateRow(RowIndex).CreateCell(CellIndex);
cell.SetCellValue(LinkName);
HSSFHyperlink link;
switch (s1.ToString())
{
case "网址": link = new HSSFHyperlink(HyperlinkType.Url);
ResultLinkValue = string.Copy(LinkValueOrIndex);
break;
case "档案": link = new HSSFHyperlink(HyperlinkType.File);
ResultLinkValue = string.Copy(LinkValueOrIndex);
break;
case "邮件": link = new HSSFHyperlink(HyperlinkType.Email);
// ResultLinkValue = string.Copy(LinkValue);
ResultLinkValue = "mailto:" + LinkValueOrIndex;
break;
case "内容":
int result;
link = new HSSFHyperlink(HyperlinkType.Document);
if (int.TryParse(LinkValueOrIndex, out result) == true)
ResultLinkValue = "'" + workbook.GetSheetName(result) + "'!A1";
else
ResultLinkValue = "'" + LinkValueOrIndex + "'!A1";
break;
default: link = new HSSFHyperlink(HyperlinkType.Url);
break;
}
link.Address = (ResultLinkValue);
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 在檔案儲存格中建立超連結.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetNameOrIndex">Index of the sheet name or.</param>
/// <param name="LinkName">Name of the link.</param>
/// <param name="LinkValueOrIndex">Index of the link value or.</param>
/// <param name="s1">The s1.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="CellIndex">Index of the cell.</param>
public static void MakeLink(string FileName, Stream InputStream, string SheetNameOrIndex, string LinkName, string LinkValueOrIndex, LinkType s1, int RowIndex, int CellIndex)
{
MemoryStream ms = MakeLink(InputStream, SheetNameOrIndex, LinkName, LinkValueOrIndex, s1, RowIndex, CellIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 建立新位元流並在儲存格中建立超連結.
/// </summary>
/// <param name="SheetNameOrIndex">Index of the sheet name or.</param>
/// <param name="LinkName">Name of the link.</param>
/// <param name="LinkValueOrIndex">Index of the link value or.</param>
/// <param name="s1">The s1.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="CellIndex">Index of the cell.</param>
/// <returns></returns>
public static Stream MakeLinkFromEmpty(string SheetNameOrIndex, string LinkName, string LinkValueOrIndex, LinkType s1, int RowIndex, int CellIndex)
{ workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet();
//ISheet sheet = hssfworkbook.CreateSheet("Hyperlinks");
////cell style for hyperlinks
////by default hyperlinks are blue and underlined
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ICellStyle hlink_style = workbook.CreateCellStyle();
IFont hlink_font = workbook.CreateFont();
hlink_font.Underline = FontUnderlineType.Single;
hlink_font.Color = IndexedColors.Blue.Index;
hlink_style.SetFont(hlink_font);
string ResultLinkValue = string.Empty;
int ResultSheet;
ISheet sheet;
if (int.TryParse(SheetNameOrIndex, out ResultSheet) == true)
sheet = workbook.GetSheetAt(ResultSheet);
else
sheet = workbook.GetSheet(SheetNameOrIndex);
ICell cell = sheet.CreateRow(RowIndex).CreateCell(CellIndex);
cell.SetCellValue(LinkName);
HSSFHyperlink link;
switch (s1.ToString())
{
case "网址": link = new HSSFHyperlink(HyperlinkType.Url);
ResultLinkValue = string.Copy(LinkValueOrIndex);
break;
case "档案": link = new HSSFHyperlink(HyperlinkType.File);
ResultLinkValue = string.Copy(LinkValueOrIndex);
break;
case "邮件": link = new HSSFHyperlink(HyperlinkType.Email);
// ResultLinkValue = string.Copy(LinkValue);
ResultLinkValue = "mailto:" + LinkValueOrIndex;
break;
case "内容":
int result;
link = new HSSFHyperlink(HyperlinkType.Document);
if (int.TryParse(LinkValueOrIndex, out result) == true)
ResultLinkValue = "'" + workbook.GetSheetName(result) + "'!A1";
else
ResultLinkValue = "'" + LinkValueOrIndex + "'!A1";
break;
default: link = new HSSFHyperlink(HyperlinkType.Url);
break;
}
link.Address = (ResultLinkValue);
cell.Hyperlink = (link);
cell.CellStyle = (hlink_style);
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 建立新檔案並在儲存格中建立超連結.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="SheetNameOrIndex">Index of the sheet name or.</param>
/// <param name="LinkName">Name of the link.</param>
/// <param name="LinkValueOrIndex">Index of the link value or.</param>
/// <param name="s1">The s1.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="CellIndex">Index of the cell.</param>
public static void MakeLinkFromEmpty(string FileName, string SheetNameOrIndex, string LinkName, string LinkValueOrIndex, LinkType s1, int RowIndex, int CellIndex)
{
MemoryStream ms = MakeLinkFromEmpty(SheetNameOrIndex, LinkName, LinkValueOrIndex, s1, RowIndex, CellIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 设定字体字形
public static ICellStyle SetCellStyle(IFont InputFont)
{
InitializeWorkbook();
ICellStyle style1 = workbook.CreateCellStyle();
style1.SetFont(InputFont);
return style1;
}
/// <summary>
/// 設定字體顏色大小到位元流.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="FontName">Name of the font.</param>
/// <param name="FontSize">Size of the font.</param>
/// <param name="IsAllSheet">if set to <c>true</c> [is all sheet].</param>
/// <param name="SheetName">Name of the sheet.</param>
/// <returns></returns>
public static Stream ApplyStyleToFile(Stream InputStream, string FontName, short FontSize, bool IsAllSheet, params string[] SheetName)
{
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
IFont font = workbook.CreateFont();
ICellStyle Style = workbook.CreateCellStyle();
font.FontHeightInPoints = FontSize;
font.FontName = FontName;
Style.SetFont(font);
MemoryStream ms = new MemoryStream();
int i;
if (IsAllSheet == true)
{
for (i = ; i < workbook.NumberOfSheets; i++)
{
ISheet Sheets = workbook.GetSheetAt();
for (int k = Sheets.FirstRowNum; k <= Sheets.LastRowNum; k++)
{
IRow row = Sheets.GetRow(k);
for (int l = row.FirstCellNum; l < row.LastCellNum; l++)
{
ICell Cell = row.GetCell(l);
Cell.CellStyle = Style;
}
}
}
}
else
{
for (i = ; i < SheetName.Length; i++)
{
ISheet Sheets = workbook.GetSheet(SheetName[i]);
for (int k = Sheets.FirstRowNum; k <= Sheets.LastRowNum; k++)
{
IRow row = Sheets.GetRow(k);
for (int l = row.FirstCellNum; l < row.LastCellNum; l++)
{
ICell Cell = row.GetCell(l);
Cell.CellStyle = Style;
}
}
}
}
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 設定字體顏色大小到位元流.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="FontName">Name of the font.</param>
/// <param name="FontSize">Size of the font.</param>
/// <param name="IsAllSheet">if set to <c>true</c> [is all sheet].</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <returns></returns>
public static Stream ApplyStyleToFile(Stream InputStream, string FontName, short FontSize, bool IsAllSheet, params int[] SheetIndex)
{
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
IFont font = workbook.CreateFont();
ICellStyle Style = workbook.CreateCellStyle();
font.FontHeightInPoints = FontSize;
font.FontName = FontName;
Style.SetFont(font);
int i;
if (IsAllSheet == true)
{
for (i = ; i < workbook.NumberOfSheets; i++)
{
ISheet Sheets = workbook.GetSheetAt();
for (int k = Sheets.FirstRowNum; k <= Sheets.LastRowNum; k++)
{
IRow row = Sheets.GetRow(k);
for (int l = row.FirstCellNum; l < row.LastCellNum; l++)
{
ICell Cell = row.GetCell(l);
Cell.CellStyle = Style;
}
}
}
}
else
{
for (i = ; i < SheetIndex.Length; i++)
{
ISheet Sheets = workbook.GetSheetAt(SheetIndex[i]);
for (int k = Sheets.FirstRowNum; k <= Sheets.LastRowNum; k++)
{
IRow row = Sheets.GetRow(k);
for (int l = row.FirstCellNum; l < row.LastCellNum; l++)
{
ICell Cell = row.GetCell(l);
Cell.CellStyle = Style;
}
}
}
}
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 設定字體顏色大小到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="FontName">Name of the font.</param>
/// <param name="FontSize">Size of the font.</param>
/// <param name="IsAllSheet">if set to <c>true</c> [is all sheet].</param>
/// <param name="SheetName">Name of the sheet.</param>
public static void ApplyStyleToFile(string FileName, Stream InputStream, string FontName, short FontSize, bool IsAllSheet, params string[] SheetName)
{
MemoryStream ms = ApplyStyleToFile(InputStream, FontName, FontSize, IsAllSheet, SheetName) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 設定字體顏色大小到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="FontName">Name of the font.</param>
/// <param name="FontSize">Size of the font.</param>
/// <param name="IsAllSheet">if set to <c>true</c> [is all sheet].</param>
/// <param name="SheetIndex">Index of the sheet.</param>
public static void ApplyStyleToFile(string FileName, Stream InputStream, string FontName, short FontSize, bool IsAllSheet, params int[] SheetIndex)
{
MemoryStream ms = ApplyStyleToFile(InputStream, FontName, FontSize, IsAllSheet, SheetIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 建立空白excel檔
/// <summary>
/// 建立空白excel檔到位元流.
/// </summary>
/// <param name="SheetName">Name of the sheet.</param>
/// <returns></returns>
public static Stream CreateEmptyFile(params string[] SheetName)
{
MemoryStream ms = new MemoryStream();
workbook = new HSSFWorkbook();
InitializeWorkbook();
if (SheetName == null)
{
workbook.CreateSheet();
}
else
{
foreach (string temp in SheetName)
workbook.CreateSheet(temp);
}
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 建立空白excel檔到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="SheetName">Name of the sheet.</param>
public static void CreateEmptyFile(string FileName, params string[] SheetName)
{
MemoryStream ms = CreateEmptyFile(SheetName) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 設定格線
/// <summary>
/// 設定格線到位元流.
/// </summary>
/// <param name="InputSteam">The input steam.</param>
/// <param name="haveGridLine">if set to <c>true</c> [have grid line].</param>
/// <param name="SheetName">Name of the sheet.</param>
/// <returns></returns>
public static Stream SetGridLine(Stream InputSteam, bool haveGridLine, params string[] SheetName)
{
workbook = new HSSFWorkbook(InputSteam);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
if (SheetName == null)
{
for (int i = ; i < workbook.NumberOfSheets; i++)
{
ISheet s1 = workbook.GetSheetAt(i);
s1.DisplayGridlines = haveGridLine;
}
}
else
{
foreach (string TempSheet in SheetName)
{
ISheet s1 = workbook.GetSheet(TempSheet);
s1.DisplayGridlines = haveGridLine;
}
}
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 設定格線到位元流.
/// </summary>
/// <param name="InputSteam">The input steam.</param>
/// <param name="haveGridLine">if set to <c>true</c> [have grid line].</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <returns></returns>
public static Stream SetGridLine(Stream InputSteam, bool haveGridLine, params int[] SheetIndex)
{
workbook = new HSSFWorkbook(InputSteam);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
if (SheetIndex == null)
{
for (int i = ; i < workbook.NumberOfSheets; i++)
{
ISheet s1 = workbook.GetSheetAt(i);
s1.DisplayGridlines = haveGridLine;
}
}
else
{
foreach (int TempSheet in SheetIndex)
{
ISheet s1 = workbook.GetSheetAt(TempSheet);
s1.DisplayGridlines = haveGridLine;
}
}
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 設定格線到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputSteam">The input steam.</param>
/// <param name="haveGridLine">if set to <c>true</c> [have grid line].</param>
/// <param name="SheetIndex">Index of the sheet.</param>
public static void SetGridLine(string FileName, Stream InputSteam, bool haveGridLine, params int[] SheetIndex)
{
MemoryStream ms = SetGridLine(InputSteam, haveGridLine, SheetIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 設定格線到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputSteam">The input steam.</param>
/// <param name="haveGridLine">if set to <c>true</c> [have grid line].</param>
/// <param name="SheetName">Name of the sheet.</param>
public static void SetGridLine(string FileName, Stream InputSteam, bool haveGridLine, params string[] SheetName)
{
MemoryStream ms = SetGridLine(InputSteam, haveGridLine, SheetName) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 擷取字串從excel檔案
/// <summary>
/// 從位元流將資料轉成字串輸出
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <returns></returns>
public static string ExtractStringFromFileStream(Stream InputStream)
{
HSSFWorkbook HBook = new HSSFWorkbook(InputStream);
ExcelExtractor extractor = new ExcelExtractor(HBook);
return extractor.Text;
}
/// <summary>
/// 從檔案將資料轉成字串輸出
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <returns></returns>
public static string ExtractStringFromFileStream(string FileName)
{
FileInfo fi = new FileInfo(FileName);
if (fi.Exists == true)
{
using (FileStream fs = fi.Open(FileMode.Open))
{
HSSFWorkbook HBook = new HSSFWorkbook(fs);
ExcelExtractor extractor = new ExcelExtractor(HBook);
return extractor.Text;
}
}
else return null;
}
#endregion #region 設定群組 /// <summary>
/// 設定群組到位元流.
/// </summary>
/// <param name="SheetName">Name of the sheet.</param>
/// <param name="IsRow">if set to <c>true</c> [is row].</param>
/// <param name="From">From.</param>
/// <param name="End">The end.</param>
/// <returns></returns>
public static Stream CreateGroup(string SheetName, bool IsRow, int From, int End)
{
MemoryStream ms = new MemoryStream();
workbook = new HSSFWorkbook();
InitializeWorkbook();
ISheet sh = workbook.CreateSheet(SheetName);
for (int i = ; i <= End; i++)
{
sh.CreateRow(i);
}
if (IsRow == true)
sh.GroupRow(From, End);
else
sh.GroupColumn((short)From, (short)End); workbook.Write(ms);
ms.Flush();
return ms; }
/// <summary>
/// 建立群組到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="SheetName">Name of the sheet.</param>
/// <param name="IsRow">if set to <c>true</c> [is row].</param>
/// <param name="From">From.</param>
/// <param name="End">The end.</param>
public static void CreateGroup(string FileName, string SheetName, bool IsRow, int From, int End)
{
MemoryStream ms = CreateGroup(SheetName, IsRow, From, End) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 從樣板建立檔案 /// <summary>
/// 從樣板建立位元流.
/// </summary>
/// <param name="TemplateFileName">Name of the template file.</param>
/// <returns></returns>
public static Stream CreateFileStreamFromTemplate(string TemplateFileName)
{
FileInfo fi = new FileInfo(TemplateFileName);
if (fi.Exists == true)
{
MemoryStream ms = new MemoryStream();
FileStream file = new FileStream(TemplateFileName, FileMode.Open, FileAccess.Read);
workbook = new HSSFWorkbook(file);
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "測試公司";
workbook.DocumentSummaryInformation = dsi;
////create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "測試公司Excel檔案";
si.Title = "測試公司Excel檔案";
si.Author = "killysss";
si.Comments = "謝謝您的使用!";
workbook.SummaryInformation = si;
workbook.Write(ms);
ms.Flush();
return ms;
}
else return null;
} /// <summary>
/// 從樣板建立檔案.
/// </summary>
/// <param name="TemplateFileName">Name of the template file.</param>
/// <param name="OutputFileName">Name of the output file.</param>
public static void CreateFileFromTemplate(string TemplateFileName, string OutputFileName)
{
FileInfo fi = new FileInfo(TemplateFileName);
if (fi.Exists == true)
{
MemoryStream ms = CreateFileStreamFromTemplate(TemplateFileName) as MemoryStream;
WriteSteamToFile(ms, OutputFileName);
}
else
{ }
}
#endregion #region 嵌入圖片
/// <summary>
/// Loads the image.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="wb">The wb.</param>
/// <returns></returns>
public static int LoadImage(string path, HSSFWorkbook wb)
{
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[file.Length];
file.Read(buffer, , (int)file.Length);
return wb.AddPicture(buffer, PictureType.JPEG); }
/// <summary>
/// 嵌入圖片到位元流.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="PicFileName">Name of the pic file.</param>
/// <param name="IsOriginalSize">if set to <c>true</c> [is original size].</param>
/// <param name="RowPosition">The row position.</param>
/// <returns></returns>
public static Stream EmbedImage(Stream InputStream, int SheetIndex, string PicFileName, bool IsOriginalSize, int[] RowPosition)
{
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet1 = workbook.GetSheetAt(SheetIndex);
IDrawing patriarch = sheet1.CreateDrawingPatriarch();
//create the anchor
HSSFClientAnchor anchor;
anchor = new HSSFClientAnchor(, , , ,
RowPosition[], RowPosition[], RowPosition[], RowPosition[]);
anchor.AnchorType = AnchorType.MoveDontResize;
//load the picture and get the picture index in the workbook
IPicture picture = patriarch.CreatePicture(anchor, LoadImage(PicFileName, workbook));
//Reset the image to the original size.
if (IsOriginalSize == true)
picture.Resize();
//Line Style
//picture.LineStyle = LineStyle.None;
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 嵌入圖片到檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="PicFileName">Name of the pic file.</param>
/// <param name="IsOriginalSize">if set to <c>true</c> [is original size].</param>
/// <param name="RowPosition">The row position.</param>
public static void EmbedImage(string FileName, int SheetIndex, Stream InputStream, string PicFileName, bool IsOriginalSize, int[] RowPosition)
{
MemoryStream ms = EmbedImage(InputStream, SheetIndex, PicFileName, IsOriginalSize, RowPosition) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 建立新位元流並嵌入圖片.
/// </summary>
/// <param name="PicFileName">Name of the pic file.</param>
/// <param name="IsOriginalSize">if set to <c>true</c> [is original size].</param>
/// <param name="RowPosition">The row position.</param>
/// <returns></returns>
public static Stream EmbedImage(string PicFileName, bool IsOriginalSize, int[] RowPosition)
{
workbook = new HSSFWorkbook();
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet1 = workbook.CreateSheet();
IDrawing patriarch = sheet1.CreateDrawingPatriarch();
//create the anchor
HSSFClientAnchor anchor;
anchor = new HSSFClientAnchor(, , , ,
RowPosition[], RowPosition[], RowPosition[], RowPosition[]);
anchor.AnchorType = AnchorType.MoveDontResize;
//load the picture and get the picture index in the workbook
IPicture picture = patriarch.CreatePicture(anchor, LoadImage(PicFileName, workbook));
//Reset the image to the original size.
if (IsOriginalSize == true)
picture.Resize();
//Line Style
//picture.LineStyle = LineStyle.None;
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 建立新檔案並嵌入圖片.
/// </summary>
/// <param name="ExcelFileName">Name of the excel file.</param>
/// <param name="PicFileName">Name of the pic file.</param>
/// <param name="IsOriginalSize">if set to <c>true</c> [is original size].</param>
/// <param name="RowPosition">The row position.</param>
public static void EmbedImage(string ExcelFileName, string PicFileName, bool IsOriginalSize, int[] RowPosition)
{
MemoryStream ms = EmbedImage(PicFileName, IsOriginalSize, RowPosition) as MemoryStream;
WriteSteamToFile(ms, ExcelFileName);
}
#endregion #region 合併儲存格
/// <summary>
/// 合併儲存格於位元流.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="RowFrom">The row from.</param>
/// <param name="ColumnFrom">The column from.</param>
/// <param name="RowTo">The row to.</param>
/// <param name="ColumnTo">The column to.</param>
/// <returns></returns>
public static Stream MergeCell(Stream InputStream, int SheetIndex, int RowFrom, int ColumnFrom, int RowTo, int ColumnTo)
{
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
InitializeWorkbook();
ISheet sheet1 = workbook.GetSheetAt(SheetIndex);
sheet1.AddMergedRegion(new CellRangeAddress(RowFrom, ColumnFrom, RowTo, ColumnTo));
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 合併儲存格於檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="RowFrom">The row from.</param>
/// <param name="ColumnFrom">The column from.</param>
/// <param name="RowTo">The row to.</param>
/// <param name="ColumnTo">The column to.</param>
public static void MergeCell(string FileName, Stream InputStream, int SheetIndex, int RowFrom, int ColumnFrom, int RowTo, int ColumnTo)
{
MemoryStream ms = MergeCell(InputStream, SheetIndex, RowFrom, ColumnFrom, RowTo, ColumnTo) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 建立新位元流並合併儲存格.
/// </summary>
/// <param name="RowFrom">The row from.</param>
/// <param name="ColumnFrom">The column from.</param>
/// <param name="RowTo">The row to.</param>
/// <param name="ColumnTo">The column to.</param>
/// <returns></returns>
public static Stream MergeCell(int RowFrom, int ColumnFrom, int RowTo, int ColumnTo)
{
workbook = new HSSFWorkbook();
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
InitializeWorkbook();
ISheet sheet1 = workbook.CreateSheet();
sheet1.AddMergedRegion(new CellRangeAddress(RowFrom, ColumnFrom, RowTo, ColumnTo));
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 建立新檔案並合併儲存格.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="RowFrom">The row from.</param>
/// <param name="ColumnFrom">The column from.</param>
/// <param name="RowTo">The row to.</param>
/// <param name="ColumnTo">The column to.</param>
public static void MergeCell(string FileName, int RowFrom, int ColumnFrom, int RowTo, int ColumnTo)
{
MemoryStream ms = MergeCell(RowFrom, ColumnFrom, RowTo, ColumnTo) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion #region 設定儲存格公式
/// <summary>
/// 設定儲存格公式於位元流.
/// </summary>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="Formula">The formula.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="ColumnIndex">Index of the column.</param>
/// <returns></returns>
public static Stream SetFormula(Stream InputStream, int SheetIndex, string Formula, int RowIndex, int ColumnIndex)
{
//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'
//So we insert three sheet just like what Excel does
workbook = new HSSFWorkbook(InputStream);
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet1 = workbook.GetSheetAt(SheetIndex);
sheet1.CreateRow(RowIndex).CreateCell(ColumnIndex).SetCellFormula(Formula);
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 設定儲存格公式於檔案.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="InputStream">The input stream.</param>
/// <param name="SheetIndex">Index of the sheet.</param>
/// <param name="Formula">The formula.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="ColumnIndex">Index of the column.</param>
public static void SetFormula(string FileName, Stream InputStream, int SheetIndex, string Formula, int RowIndex, int ColumnIndex)
{
MemoryStream ms = SetFormula(InputStream, SheetIndex, Formula, RowIndex, ColumnIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
/// <summary>
/// 建立新位元流並設定儲存格公式.
/// </summary>
/// <param name="Formula">The formula.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="ColumnIndex">Index of the column.</param>
/// <returns></returns>
public static Stream SetFormula(string Formula, int RowIndex, int ColumnIndex)
{
//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'
//So we insert three sheet just like what Excel does
workbook = new HSSFWorkbook();
InitializeWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet1 = workbook.CreateSheet();
sheet1.CreateRow(RowIndex).CreateCell(ColumnIndex).SetCellFormula(Formula);
workbook.Write(ms);
ms.Flush();
return ms;
}
/// <summary>
/// 建立新檔案並設定儲存格公式.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="Formula">The formula.</param>
/// <param name="RowIndex">Index of the row.</param>
/// <param name="ColumnIndex">Index of the column.</param>
public static void SetFormula(string FileName, string Formula, int RowIndex, int ColumnIndex)
{
MemoryStream ms = SetFormula(Formula, RowIndex, ColumnIndex) as MemoryStream;
WriteSteamToFile(ms, FileName);
}
#endregion
}

NPOI操作Excel辅助类的更多相关文章

  1. NPOI操作excel之写入数据到excel表

    在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...

  2. C&num;开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  3. 用NPOI操作EXCEL关于HSSFClientAnchor&lpar;dx1&comma;dy1&comma;dx2&comma;dy2&comma;col1&comma;row1&comma;col2&comma;row2&rpar;的参数

    2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数   NPOI教程:http://www.cnb ...

  4. C&num; 如何使用NPOI操作Excel以及读取合并单元格等

    C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...

  5. 用NPOI操作EXCEL-锁定列CreateFreezePane&lpar;&rpar;

    public void ExportPermissionRoleData(string search, int roleStatus) { var workbook = new HSSFWorkboo ...

  6. &period;NET 通过 NPOI 操作 Excel

    目录 .NET 通过 NPOI 操作 Excel 第一步:通过 NuGet 获取 NPOI 包并引入程序集 第二步:引入 NPOI 帮助类 第三步:在程序中调用相应的方法对数据进行导出导入操作 将 D ...

  7. 2&period;6&period;2 用NPOI操作EXCEL--设置密码才可以修改单元格内容

    2.6.2 用NPOI操作EXCEL--设置密码       有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完 ...

  8. 使用NPOI操作Excel文件及其日期处理

    工作中经常遇到需要读取或导出Excel文件的情况,而NPOI是目前最宜用.效率最高的操作的Office(不只是Excel哟)文件的组件,使用方便,不详细说明了. Excel工作表约定:整个Excel表 ...

  9. C&num;通过NPOI操作Excel

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...

随机推荐

  1. 《TCP&sol;IP高效编程:改善网络程序的44个技巧》源码在Linux上的编译

    为了先完整编译通过,需要以下几个步骤: 1.进入linux子目录,执行 make.(此步骤为打补丁.) 2.返回上一级目录,打开文件 simplec.c,添加头文件 #include <stdi ...

  2. QA&colon;java&period;lang&period;RuntimeException&colon;java&period;io&period;FileNotFoundException&colon;Resource nexus-maven-repository-index&period;properties does not exist&period;

    QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.pr ...

  3. 解决vs2015使用fopen、fprintf等函数报错的问题

    出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...

  4. Swift-Lesson2&lpar;下&rpar;

  5. 以setTimeout来聊聊Event Loop

    平时的工作中,也许你会经常用到setTimeout这个方法,可是你真的了解setTimeout吗?本文想通过总结setTimeout的用法,顺便来探索javascript里面的事件执行机制. setT ...

  6. Django之Cookie和Session

    http://www.cnblogs.com/liwenzhou/p/8343243.html 一.Cookie Cookie是什么? 就是保存在客户端浏览器上的键值对. Cookie为什么存在? 因 ...

  7. Vue 目录结构 绑定数据 绑定属性 循环渲染数据

    一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...

  8. tomcat的JVM调优

    1.error场景 Tomcat 长期运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或java.lang.OutOfMemoryE ...

  9. C&num; 选项卡控件

    选项卡控件,它提供一系列操作按钮,单击不同的按钮可以在各个页面之间进行切换. 在Windows Form应用程序中,选项卡控件即“TebPage”控件,它公开“TebPage”属性,表示一个由“Tab ...

  10. DevOps - CI - Sonar

    Sonar 官方信息 https://www.sonarqube.org/ https://www.sonarqube.org/downloads/ https://docs.sonarqube.or ...