iT邦幫忙

0

C# NPOI excel 儲存格疑問,和字形顏色,換行

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20180611/20109425MIJjuxvrlB.jpg

上面是我Excel底板

請問我合併儲存格和字的顏色怎麼寫呢?
和空格顏色

HSSFWorkbook workbook;
            
            //讀取專案內中的ZZZ.xls 的excel 檔案
            using (FileStream file = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "ZZZ.xls", FileMode.Open, FileAccess.Read))
         
                
            {
                
                workbook = new HSSFWorkbook(file);
                
            }

            //讀取Sheet1 工作表
            var  sheet = workbook.GetSheet("工作表");

            
            for (int row = 0; row <= sheet.LastRowNum; row++)
            {
                if (sheet.GetRow(row) != null) //null is when the row only contains empty cells 
                {
                    foreach (var c in sheet.GetRow(row).Cells)
                    {
                        //如果是數字型 就要取 NumericCellValue  這屬性的值
                        if(c.CellType==CellType.NUMERIC)
                            Response.Write(c.NumericCellValue.ToString("#"));

                        //如果是字串型 就要取 StringCellValue  這屬性的值
                        if(c.CellType==CellType.STRING)
                            Response.Write(c.StringCellValue);

                        //每個欄中間加上逗點
                        Response.Write(",");
                        //合併儲存格
                         CellRangeAddress region = new CellRangeAddress(0, 0, 1, 8);
                        sheet.AddMergedRegion(region);
                         }
                          Response.Write("<br />");
                        
ted8224 iT邦新手 5 級 ‧ 2018-06-11 17:01:15 檢舉
顯示文字部分是可以
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
焦阿
iT邦新手 5 級 ‧ 2018-06-12 09:03:48
ted8224 iT邦新手 5 級 ‧ 2018-06-12 13:24:36 檢舉
 CellRangeAddress region = new CellRangeAddress(0, 0, 1, 8);
                    sheet.AddMergedRegion(region);

請問
那上面是合併儲存格嗎?
我是發現您給的連結 上面code
好像是創造

我是讀取本身excel檔 狀態這樣

我要發表回答

立即登入回答