佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 使用Aspose.Cells組件生成Excel文件實(shí)例

使用Aspose.Cells組件生成Excel文件實(shí)例

熱門標(biāo)簽:電銷業(yè)務(wù) 國(guó)美全國(guó)運(yùn)營(yíng)中心 客戶服務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 人工智能 電商新玩法 網(wǎng)站排名優(yōu)化 百度AI接口

生成帶表頭的Excel文件,格式如下顯示。

當(dāng)然更復(fù)雜的一些也可以通過(guò) 合并單元格的方法 public void Merge(int firstRow, int firstColumn, int totalRows, int totalColumns)來(lái)實(shí)現(xiàn)。

實(shí)現(xiàn)方式:

1. 首先,需要添加對(duì)"Aspose.Cells.dll"的引用。

2. 實(shí)現(xiàn)代碼如下:

復(fù)制代碼 代碼如下:

//新建工作簿
            Workbook workbook = new Workbook(); //工作簿
            Worksheet sheet = workbook.Worksheets[0]; //工作表
            Cells cells = sheet.Cells;//單元格


            Style style = workbook.Styles[workbook.Styles.Add()];//新增樣式

            #region 表頭
            //標(biāo)題
            style.HorizontalAlignment = TextAlignmentType.Center;//文字居中 
            style.Font.Name = "宋體";//文字字體
            style.Font.Size = 18;//文字大小 
            style.Font.IsBold = true;//粗體

            cells.Merge(0, 0, 1, 12);               //合并單元格
            cells[0, 0].PutValue("標(biāo)準(zhǔn)化工作意見(jiàn)建議匯總表");   //填寫(xiě)內(nèi)容
            cells[0, 0].SetStyle(style);            //給單元格關(guān)聯(lián)樣式 
            cells.SetRowHeight(0, 28);              //設(shè)置行高 


            //發(fā)布時(shí)間
            style.HorizontalAlignment = TextAlignmentType.Left;
            style.Font.Size = 11;
            style.Font.IsBold = false;
            cells.Merge(1, 0, 1, 7);
            cells[1, 0].PutValue(String.Format("發(fā)布起止時(shí)間:{0}至{1}",DateTime.Now.AddDays(-1).ToString("yyyy年MM月dd日"),DateTime.Now.ToString("yyyy年MM月dd日")));
            cells[1, 0].SetStyle(style);
            cells.SetRowHeight(1, 20);

            //統(tǒng)計(jì)時(shí)間
            style.HorizontalAlignment = TextAlignmentType.Right;
            style.Font.Size = 11;
            style.Font.IsBold = false;
            cells.Merge(1, 7, 1, 5);
            cells[1, 7].PutValue(String.Format("統(tǒng)計(jì)時(shí)間:{0}", DateTime.Now.ToString("yyyy年MM月dd日")));
            cells[1, 7].SetStyle(style);
            cells.SetRowHeight(1, 20);
            #endregion

            #region 表格

            #region 表格標(biāo)題行
            //序號(hào)
            style.HorizontalAlignment = TextAlignmentType.Center;
            cells[2, 0].PutValue("序號(hào)");
            cells[2, 0].SetStyle(style);
            cells.SetRowHeight(2, 20);
            cells.SetColumnWidthPixel(0, 38);

            //建議時(shí)間
            cells[2, 1].PutValue("建議時(shí)間");
            cells[2, 1].SetStyle(style);
            cells.SetColumnWidthPixel(1, 77);

            //建議部門
            cells[2, 2].PutValue("建議部門");
            cells[2, 2].SetStyle(style);
            cells.SetColumnWidthPixel(2, 107);

            //建 議 人
            cells[2, 3].PutValue("建 議 人");
            cells[2, 3].SetStyle(style);
            cells.SetColumnWidthPixel(3, 69);

            //類   別
            cells[2, 4].PutValue("類   別");
            cells[2, 4].SetStyle(style);
            cells.SetColumnWidthPixel(4, 71);

            //業(yè)務(wù)種類
            cells[2, 5].PutValue("業(yè)務(wù)種類");
            cells[2, 5].SetStyle(style);
            cells.SetColumnWidthPixel(5, 71);

            //標(biāo)準(zhǔn)名稱
            cells[2, 6].PutValue("標(biāo)準(zhǔn)名稱");
            cells[2, 6].SetStyle(style);
            cells.SetColumnWidthPixel(6, 114);

            //標(biāo)準(zhǔn)章、條編號(hào)
            cells[2, 7].PutValue("標(biāo)準(zhǔn)章、條編號(hào)");
            cells[2, 7].SetStyle(style);
            cells.SetColumnWidthPixel(7, 104);

            //意見(jiàn)建議
            cells[2, 8].PutValue("意見(jiàn)建議");
            cells[2, 8].SetStyle(style);
            cells.SetColumnWidthPixel(8, 255);

            //處理部門
            cells[2, 9].PutValue("處理部門");
            cells[2, 9].SetStyle(style);
            cells.SetColumnWidthPixel(9, 72);

            //處理進(jìn)度
            cells[2, 10].PutValue("處理進(jìn)度");
            cells[2, 10].SetStyle(style);
            cells.SetColumnWidthPixel(10, 72);

            //備注
            cells[2, 11].PutValue("備注");
            cells[2, 11].SetStyle(style);
            cells.SetColumnWidthPixel(11, 255);

            #endregion

            #endregion


            System.IO.MemoryStream ms = workbook.SaveToStream();//生成數(shù)據(jù)流
            byte[] bt = ms.ToArray();

            workbook.Save(@"E:\test.xls");//保存到硬盤
        }

3. 生成好的Excel可以保存到磁盤,也可以在web頁(yè)面上通過(guò)流的方式來(lái)下載。

復(fù)制代碼 代碼如下:

//下載
            System.IO.MemoryStream ms = workbook.SaveToStream();//生成數(shù)據(jù)流
            byte[] bt = ms.ToArray();

            string fileName = "標(biāo)準(zhǔn)化工作意見(jiàn)建議匯總表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//客戶端保存的文件名
            //以字符流的形式下載文件  

            Response.ContentType = "application/vnd.ms-excel";

            //通知瀏覽器下載文件而不是打開(kāi)
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bt);

            Response.Flush();
            Response.End();

您可能感興趣的文章:
  • 利用Aspose.Cells和Excel模板導(dǎo)出統(tǒng)計(jì)數(shù)據(jù)
  • Aspose.Cells組件導(dǎo)出excel文件
  • C#使用Aspose.Cells導(dǎo)出excel
  • C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
  • Aspose.Cells 讀取受保護(hù)有密碼的Excel文件
  • C#使用Aspose.Cells控件讀取Excel
  • C#語(yǔ)言MVC框架Aspose.Cells控件導(dǎo)出Excel表數(shù)據(jù)

標(biāo)簽:廈門 咸寧 POS機(jī) 益陽(yáng) 拉薩 南平 攀枝花 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Aspose.Cells組件生成Excel文件實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    玛多县| 扬州市| 平潭县| 大城县| 宁安市| 嘉黎县| 遵化市| 抚宁县| 什邡市| 湖口县| 颍上县| 三河市| 英德市| 喜德县| 潞西市| 江陵县| 蓬安县| 炉霍县| 阿拉善右旗| 拜泉县| 辰溪县| 进贤县| 安仁县| 普兰县| 磐石市| 凯里市| 汉川市| 东乌| 休宁县| 潼南县| 山阴县| 永修县| 雷波县| 林州市| 南和县| 东宁县| 宝鸡市| 寻乌县| 措勤县| 沙雅县| 融水|