NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using NPOI.HSSF.UserModel;
using NPOI.SS.Util;
using NPOI.SS.UserModel;
using System.IO;


///<summary>
///使用NPOI方式將DataTable匯出至Excel
///</summary>
///<param name="dt">DataTable</param>
///<param name="connString">Excel連線字串</param>
///<returns></returns>
public static void DataTableToExcel(DataTable dt, String FileName, String Title)
{
int tColumnCount = dt.Columns.Count;
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(FileName);
//顯示 所有資料列
int rowIndex = 0;
IFont fontHeader = (HSSFFont)workbook.CreateFont();
ICellStyle HeaderStyle = workbook.CreateCellStyle();
HeaderStyle.WrapText = false;
HeaderStyle.Alignment = HorizontalAlignment.CENTER;
//背景顏色
HeaderStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LIGHT_YELLOW.index;
HeaderStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
//以下為設定粗體字
fontHeader.Boldweight = (short)FontBoldWeight.BOLD;
fontHeader.FontHeightInPoints = (short)Convert.ToUInt32((12));
fontHeader.FontName = "細明體";
HeaderStyle.SetFont(fontHeader);
if (Title != "")
{
IRow titleRow = sheet.CreateRow(rowIndex);
titleRow.CreateCell(0);
ICell cell = titleRow.CreateCell(0);
cell.SetCellValue(Title);
rowIndex++;
ICellStyle style = workbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
IFont font = workbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cell.CellStyle = style;

sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, tColumnCount-1));
}


//顯示 Table 0 的所有欄位名稱
IRow headerRow = sheet.CreateRow(rowIndex);
rowIndex++;
foreach (DataColumn column in dt.Columns)
{
ICell headecell = headerRow.CreateCell(column.Ordinal);
headecell.SetCellValue(column.ColumnName);
headecell.CellStyle = HeaderStyle;
//自動Width
//sheet.AutoSizeColumn(column.Ordinal);
}

foreach (DataRow row in dt.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in dt.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
dataRow = null;
rowIndex++;
}

foreach (DataColumn column in dt.Columns)
{
//自動Width
sheet.AutoSizeColumn(column.Ordinal);
}

HttpContext.Current.Response.Clear();

// 產生 Excel 資料流
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
headerRow = null;
sheet = null;
workbook = null;

HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.Default;

// 設定強制下載標頭
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + FileName + ".xls"));
// 輸出檔案
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
HttpContext.Current.Response.End();
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.