Skip to main content

Posts

Showing posts with the label C#

C# 轉換中文2位元/截斷多於文字

// 超過 10bytes 則截斷    例: title= 超過 10bytes 則截斷 var titlebytes = title.Length; var minbyts = 10; for (var i = 0; i < title.Length; i++) if (((int)title[i]) > 127) { titlebytes++; minbyts--; }   //(轉換) 中文字為 2 位元 if (titlebytes > 10) { title = title.Substring(0, minbyts); } else { title = title.Substring(0, title.Length); }   // 截斷多餘文字 結果: 超過 10byte

C# 執行exe檔匯出Excel至本機

using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; try {     // 匯出 EXCEL     int i = 0;    HSSFWorkbook book = new HSSFWorkbook();    ISheet sheet = book.CreateSheet(" 匯出資料 ");    IRow row;    IFont boldfont = book.CreateFont();    boldfont.Boldweight = (short)FontBoldWeight.Bold;   // 字型    ICellStyle cellstyle = book.CreateCellStyle();    cellstyle.SetFont(boldfont);    int row_index, col_index;    FileStream file = new FileStream(@"C:\Users\**\Desktop\excel1.xls", FileMode.Create);  // 產生檔案(指定產生路徑)     MemoryStream out_file = new MemoryStream();    /////sheet1    sheet.CreateRow(0).CreateCell(0).SetCellValue("  轉檔時間 : " + DateTime.Now);    sheet.GetRow(0).GetCell(0).CellStyle = cellstyle;    //Header: 匯出題目欄位   ...

C# Browser匯出Excel 及 建立第2個sheet

建立 SHEET 方法 範例 : ISheet sheet1 = book.CreateSheet("SHEET_NAME1");  // 開第一個 SHEET   shee1...... ISheet sheet2 = book.CreateSheet("SHEET_NAME2");  // 開第二個 SHEET   sheet2...... ---程式碼案例------------------------------------------------------------------------------------------------------- using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; HSSFWorkbook book = new HSSFWorkbook (); ISheet sheet = book.CreateSheet( " sheet1 " );   //sheet 1 IRow row; IFont boldfont = book.CreateFont(); boldfont.Boldweight = ( short ) FontBoldWeight .BOLD; //設定 字型 ICellStyle cellstyle = book.CreateCellStyle(); cellstyle.SetFont(boldfont); int row_index, col_index;   //sheet1的row&col參數宣告 dt = db.ExecuteFillToDataTable(txtSQL);                          ///// sheet1內容 sheet.CreateRow(0).CreateCell(0).Set...