Skip to main content

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).SetCellValue(dt.Rows[0]["theme"].ToString() + 轉檔時間: " + DateTime.Now);   //第一列
sheet.GetRow(0).GetCell(0).CellStyle = cellstyle;

//Header:匯出題目欄位
row = sheet.CreateRow(1);   //第二列
row.CreateCell(0).SetCellValue("項次");
row.CreateCell(1).SetCellValue("編號");
row.CreateCell(2).SetCellValue("廠商");
ow.CreateCell(3).SetCellValue("聯絡人");
row.CreateCell(i + 4).SetCellValue("填寫時間"); 
 for (int i = 0; i <= dt.Rows.Count; i++)
{
    if (i == dt.Rows.Count) { row.CreateCell(i + 4).SetCellValue("填寫時間");  break; }
    id_list.Add(dt.Rows[i]["no."].ToString());
    row.CreateCell(i + 4).SetCellValue(dt.Rows[i]["no."].ToString() + "." + dt.Rows[i]["question"].ToString());
}
                          
row.RowStyle = cellstyle;  

//回答內容
row_index = 2;
DataTable dt_user= db.ExecuteFillToDataTable("SELECT From TABLE_A);
for (int i = 0; i < dt_user.Rows.Count; i++)
{
   //最後輸出所填答案
   col_index = 0;
   row = sheet.CreateRow(row_index++);
   row.CreateCell(col_index).SetCellValue(i+1);
   row.CreateCell(++col_index).SetCellValue(dt_user.Rows[i]["aaa"].ToString());
   row.CreateCell(++col_index).SetCellValue(dt_user.Rows[i]["bbb"].ToString());
   row.CreateCell(++col_index).SetCellValue(dt_user.Rows[i]["ccc"].ToString());
   row.CreateCell(++col_index).SetCellValue(dt_user.Rows[i]["ddd"].ToString()); 
}


//////SHEET2
ISheet sheet2 = book.CreateSheet("sheet2");   //create sheet 2
sheet2.CreateRow(0).CreateCell(0).SetCellValue(轉檔時間: " + DateTime.Now);  //第一列
sheet2.GetRow(0).GetCell(0).CellStyle = cellstyle;
int row_index2, col_index2;    //宣告參數
//Header:匯出題目欄位
row = sheet2.CreateRow(1);    //第二列
row.CreateCell(0).SetCellValue("項次");
row.CreateCell(1).SetCellValue("編號");
row.CreateCell(2).SetCellValue("廠商");
row.CreateCell(3).SetCellValue("聯絡人");
row.CreateCell(4).SetCellValue("E-MAIL");
row.RowStyle = cellstyle;

row_index2 = 2;    //第三列開始
                        
col_index2 = 0;
DataTable dt_noans = db.ExecuteFillToDataTable("SELECT From TABLE_B);
for (int i = 0; i < dt_noans.Rows.Count; i++)
{
  row = sheet2.CreateRow(row_index2++);
  row.CreateCell(col_index2).SetCellValue(i + 1);
  row.CreateCell(++col_index2).SetCellValue(dt_noans.Rows[i]["aaa"].ToString());
  row.CreateCell(++col_index2).SetCellValue(dt_noans.Rows[i]["bbb"].ToString());
  row.CreateCell(++col_index2).SetCellValue(dt_noans.Rows[i]["ccc"].ToString());
  row.CreateCell(++col_index2).SetCellValue(dt_noans.Rows[i]["ddd"].ToString());
 }

string outputFile = "excel.xls";
Response.ContentType = "application/excel";
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; " + "filename=\"" + outputFile + "\"; ");

MemoryStream out_file = new MemoryStream();
book.Write(out_file);
Response.BinaryWrite(out_file.ToArray());
out_file.Close();

Response.End();

Comments

Popular posts from this blog

我的救命靈藥。預防感冒的家庭常備藥

身為一位只要水分不足或講太多話就會喉嚨不適 甚至誘發感冒徵兆的人 身邊總是要常備一些良藥 (其實水就是最好的良藥阿) 以下就來介紹各種可舒緩喉嚨不適的產品,也順便介紹幾個其他類型我覺得也不錯的商品

不專業。Delphi x 電子發票證明聯 x ESC/POS指令 x WP-T810出據機 x 熱感紙列印

這幾年遇到政府推動電子發票 千辛萬苦之後,總算是解決了問題 日後會找時間做個完整的紀錄,目前先分享一些指令,也會慢慢將註解補期 關於ESC/POS指令,我找了非常多範本,統整如下 大至分為ASCII、16進位hex 以及10進位DEC 如果遇到沒有反應的指令,我就會直接換另一種方式(例如ASCII沒反應就換成16進位hex) 交叉寫,當然是很不好的示範,總而言之 各指令範例如下      //ResetPrinter------------------------------------------------------------      ResetPrinter_ASCII =  chr(27) + '@';      // 設定位置(置中、靠右、靠左)------------------------------------      AlignLeft_ASCII    = chr(27) + 'a' + chr(00); // Align Text to the Left      AlignCenter_ASCII  = chr(27) + 'a' + chr(01); // Align center.      AlignRight_ASCII   = chr(27) + 'a' + chr(02); // Align Right      decSelectAlignLeft = AnsiChar(27) + 'a' + AnsiChar(0);      decSelectAlignCenter = AnsiChar(27) + 'a' + AnsiChar(1);      decSelectAlignRight = AnsiChar(27) + 'a' + AnsiChar(2);          hexSelectAlignLeft = AnsiChar($1B) + 'a' + AnsiChar($0);      hexSelectAlignCenter = AnsiChar($1B) + 'a' + AnsiChar($1);      h

ORACLE SQL自訂排序(order by decode)

SELECT ID,description FROM A ORDER BY ID 11     台中市 13     彰化縣 14     南投縣 21     台北市 22     新北市 23     基隆市 24     桃園縣 25     新竹縣 26     新竹市 27     苗栗縣 SELECT ID,description FROM A ORDER BY  decode (ID, '21',  1 , '22',  2 , '23',  3 , '24',  4 ), ID 21     台北市 22     新北市 23     基隆市 24     桃園縣 11     台中市 13     彰化縣 25     新竹縣 26     新竹市 27     苗栗縣 註解: T ranslate ID  '21' into the value 1, ID '22'  into the value 2