NPOI操作Excel輔助類

/// <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, 0, 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, 0, 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 = 0;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 directoryMemoryStream 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(0);// 處理標題foreach (DataColumn column in SourceTable.Columns)headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);// 處理內容int rowIndex = 1;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 = 0;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(0);// handling header.foreach (DataColumn column in SourceTable.Columns)headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);// handling value.int rowIndex = 1;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 + 1 : 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 + 1 : 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(0);PropertyInfo[] properties = typeof(T).GetProperties();int columIndex = 0;foreach (PropertyInfo column in properties){headerRow.CreateCell(columIndex).SetCellValue(column.Name);columIndex++;}int rowIndex = 1;foreach (T item in SourceList){IRow dataRow = sheet.CreateRow(rowIndex);columIndex = 0;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 = 0;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(0);PropertyInfo[] properties = typeof(T).GetProperties();int columIndex = 0;foreach (PropertyInfo column in properties){headerRow.CreateCell(columIndex).SetCellValue(head[column.Name] == null ? column.Name : head[column.Name].ToString());columIndex++;}int rowIndex = 1;foreach (T item in SourceList){IRow dataRow = sheet.CreateRow(rowIndex);columIndex = 0;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 = 0;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(0);PropertyInfo[] properties = typeof(T).GetProperties();int columIndex = 0;foreach (PropertyInfo column in properties){headerRow.CreateCell(columIndex).SetCellValue(column.Name);columIndex++;}int rowIndex = 1;foreach (T item in SourceList){IRow dataRow = sheet.CreateRow(rowIndex);columIndex = 0;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(0);PropertyInfo[] properties = typeof(T).GetProperties();int columIndex = 0;foreach (PropertyInfo column in properties){headerRow.CreateCell(columIndex).SetCellValue(head[column.Name] == null ? column.Name : head[column.Name].ToString());columIndex++;}int rowIndex = 1;foreach (T item in SourceList){IRow dataRow = sheet.CreateRow(rowIndex);columIndex = 0;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 + 1; 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 >= 0 && 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 + 1; 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 >= 0 && 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 + 1; 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 >= 0 && 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 + 1; 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 >= 0 && 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 == 1){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 = 0; i < TempColName.Length; i++){DataColumn c1 = new DataColumn(TempColName[i].ToString().Trim(), typeof(object));ResultTable.Columns.Add(c1);}if (value != null){for (i = 0; i < value.GetLength(0); i++){DataRow newrow = ResultTable.NewRow();for (j = 0; 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 = 0; i < dt.Rows.Count; i++){for (int j = 0; 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 + 1 : 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 + 1 : 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);elsesheet = 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";elseResultLinkValue = "'" + 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);elsesheet = 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";elseResultLinkValue = "'" + 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 = 0; i < workbook.NumberOfSheets; i++){ISheet Sheets = workbook.GetSheetAt(0);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 = 0; 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 = 0; i < workbook.NumberOfSheets; i++){ISheet Sheets = workbook.GetSheetAt(0);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 = 0; 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 = 0; 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 = 0; 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 = 0; i <= End; i++){sh.CreateRow(i);}if (IsRow == true)sh.GroupRow(From, End);elsesh.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 SummaryInformationSummaryInformation 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, 0, (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(0, 0, 0, 0,RowPosition[0], RowPosition[1], RowPosition[2], RowPosition[3]);anchor.AnchorType = AnchorType.MoveDontResize;//load the picture and get the picture index in the workbookIPicture 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(0, 0, 0, 0,RowPosition[0], RowPosition[1], RowPosition[2], RowPosition[3]);anchor.AnchorType = AnchorType.MoveDontResize;//load the picture and get the picture index in the workbookIPicture 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 doesworkbook = 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 doesworkbook = 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
}

轉載于:https://www.cnblogs.com/weiweixiang/p/5690981.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/372615.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/372615.shtml
英文地址,請注明出處:http://en.pswp.cn/news/372615.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JSP九大內置對象,七大動作,三大指令

JSP之九大內置對象 隱藏對象入門探索 Servlet 和JSP中輸出數據都需要使用out對象。Servlet 中的out對象是通過getWriter()方法獲取的。而JSP中沒有定義out對象卻可以直接使用。這是因為out是JSO的內置隱藏對象。JSP中的常見的9個內置隱藏對象如下 out輸出流對象 隱藏對象out是…

matlab變步長的梯形公式,用變步長梯形法計算積分∫sinx/x*x的近似值(二分二次即可)...

共回答了20個問題采納率&#xff1a;90%題目沒寫全吧?現假定積分區間為[0,1],教材《數值分析》(華中科技大學出版)第87頁,例題4.2就有詳細的解答.貌似題目也有錯?確定分母是【x^2】?現在附上該例題(分母是x)的全部解答過程&#xff1a;先對整個區間[0,1]使用梯形公式.對于函…

使用Cloud SQL的Google App Engine全文搜索

許多Google AppEngine開發人員一直在等待全文搜索功能&#xff0c;特別是來自網絡上最大的搜索引擎Google。 很高興看到Google團隊正在努力&#xff0c;您可以在Google I / O 2011大會上簽到&#xff1a; Bo Majewski和Ged Ellis進行的全文本搜索 。 據我所知&#xff0c;非常有…

php 數組值sum,php sum數組值(如果特定列的值重復)

我有一個陣列。我要檢查是否有重復的費用,如果有,我要用相同的費用列匯總所有值。[12] > Array([type] > Other Miscellaneous Fees[fee] > 158[amount] > -22.56[code] > COL_AUDIO[feedesc] > COLLEGE AUDIO VISUAL FEE)[13] > Array([type] > Other…

hdu-5734 Acperience(數學)

題目鏈接&#xff1a; Acperience Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem DescriptionDeep neural networks (DNN) have shown significant improvements in several application domains including computer vision and…

Ninject依賴注入(一)

Ninject學習筆記&#xff08;一&#xff09; Ninject學習筆記&#xff08;一&#xff09;理解依賴注入DI概念什么是DI&#xff1f;DI是如何工作的&#xff1f;什么是DI容器使用Ninject如何使用NinjectNinject對象生命周期暫時范圍單例范圍線程范圍請求范圍自定義范圍Ninject模塊…

我如何向團隊解釋依賴注入

最近&#xff0c;我們公司開始開發基于Java的新Web應用程序&#xff0c;經過一些評估過程&#xff0c;我們決定使用Spring。 但是許多團隊成員并不了解Spring和Dependency Injection的原理。 因此&#xff0c;我被要求給出一個速成班&#xff0c;介紹什么是Spring上的依賴注入和…

可以添加自定義的Select控件

1.控件dom <select name"WebSiteTarget" id"WebSiteTarget" class"w1" onchange"editable2(this);"><option value"-1">請選擇城市</option><option>福州</option><option>廈門</op…

innodb_io_capacity =innodb_lru_scan_depth*inoodb_buffer_pool_instances。與 checkpoint

innodb_lru_scan_depth:每個緩沖池刷臟頁的能力 innodb_io_capacity: iops inoodb_buffer_pool_instances8 :緩沖池的個數 .關系&#xff1a; innodb_io_capacity > innodb_lru_scan_depth * inoodb_buffer_pool_instances 轉載于:https://www.cnblogs.com/zengkefu/…

Java中的責任鏈模式

當應有幾個處理器來執行某項操作并為這些處理器定義特定順序時&#xff0c;就需要采用責任鏈設計模式。 在運行時處理器順序的可變性也很重要。模式的UML表示如下&#xff1a; 處理程序定義處理器對象的一般結構。 這里的“ HandleRequest”是抽象處理器方法。 處理程序還具有自…

php的excel源碼下載,PHPExcel-5 - 源碼下載|Windows編程|其他小程序|源代碼 - 源碼中國...

文件名大小更新時間PHPExcel02019-05-11PHPExcel\.gitattributes702019-01-02PHPExcel\.gitignore1082019-01-02PHPExcel\.travis.yml5122019-01-02PHPExcel\16329.xlsx510662019-05-11PHPExcel\19093.xlsx511932019-05-11PHPExcel\43877.xlsx530952019-05-11PHPExcel\62045.xl…

使用Visual Studio Code開發Asp.Net Core WebApi學習筆記(六)-- 依賴注入

本篇將介紹Asp.Net Core中一個非常重要的特性&#xff1a;依賴注入&#xff0c;并展示其簡單用法。 第一部分、概念介紹 Dependency Injection&#xff1a;又稱依賴注入&#xff0c;簡稱DI。在以前的開發方式中&#xff0c;層與層之間、類與類之間都是通過new一個對方的實例進行…

基于JAX-WS的webService開發實例

最近因為工作原因接觸到webService&#xff0c;所以記錄下開發中碰到的問題&#xff0c;方便自己以后復習&#xff0c;順便發揚一下開源精神。剛剛接觸webServie如果有什么錯誤歡迎大家指正。 本地環境&#xff1a;myEclipse10.6 tomcat7 JDK7 jaxws-ri-2.2.10 第一步&#xff…

完整的WebApplication JSF EJB JPA JAAS –第2部分

視圖–創建和JSF設置 本教程是第1部分的繼續。 讓我們創建一個新的Dynamic Web Project 。 如下圖所示創建它&#xff1a; 注意&#xff1a;在某些時候&#xff0c;Eclipse會詢問您是否要添加JSF功能&#xff08;自動完成&#xff09;&#xff0c;然后啟用它。 就像下面的屏幕…

lempel ziv matlab,基于Python的LempelZiv算法的熵估計

此函數允許估計時間序列的熵。它基于Lempel-Ziv壓縮算法。對于長度為n的時間序列&#xff0c;熵估計為&#xff1a;E(1/n和L_i)^-1 ln(n)式中&#xff0c;L逯i是從位置i開始的最短子串的長度&#xff0c;該子串之前沒有從位置1出現到i-1。當n接近無窮大時&#xff0c;估計的熵收…

Android使用繪圖Path總結

Path作為Android中一種相對復雜的繪圖方式&#xff0c;官方文檔中的有些解釋并不是很好理解&#xff0c;這里作一個相對全面一些的總結&#xff0c;供日后查看&#xff0c;也分享給大家&#xff0c;共同進步。 1.基本繪圖方法 addArc(RectF oval, float startAngle, float swee…

2017.3.23下午

下午通過對OSPF基本原理進一步的學習&#xff0c;對上午學習的內容進行了復習。 轉載于:https://www.cnblogs.com/bgd140206206/p/6606192.html

編寫Eclipse插件教程–第1部分

Eclipse是三個最受歡迎的Java開發IDE之一。 其成功的原因之一是其可擴展性。 對于任何知道該怎么做并且已經做到的人來說&#xff0c;編寫eclipse插件都可以非常輕松快捷。 不幸的是&#xff0c;第一次在Eclipse中進行操作可能會非常耗時且令人沮喪。 Eclipse框架非常龐大&…

簡單Window下 Android Studio的安裝

&#xff08;1&#xff09;首先安裝JDK 下載JDK 本人覺得官方網站下JDK比較慢&#xff0c;可以直接百度JDK&#xff0c;&#xff08;如果是64位 百度搜索記得64位&#xff09; 類似于這樣的下載 安裝可以看下教程&#xff0c;包括環境變量的配置 如何安裝JDK &#xff08;2&…

日期處理一之NSLalendar的使用

一、日期和時間模式 日期和時間格式由日期和時間模式字符串組成&#xff0c;在日期和時間模式字符串中未加引號的A到‘Z’和a到‘z’被解釋為模式字母&#xff0c;用來表示日期或時間。字符串元素&#xff0c;文本可以使用單引號&#xff08;‘’&#xff09;引起來使用。定義以…