using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Aspose.Cells;
using System.IO;
using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

/// <summary>
        /// 生成EXCEL
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

dt.Columns.Add("Product", Type.GetType("System.String"));
            dt.Columns.Add("Version", Type.GetType("System.String"));
            dt.Columns.Add("Description", Type.GetType("System.String"));

DataRow newRow;
            newRow = dt.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            newRow["Description"] = "我很喜欢";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "4.0";
            newRow["Description"] = "我很喜欢";
            dt.Rows.Add(newRow);

newRow = dt.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.1";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.1";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);

newRow = dt.NewRow();
            newRow["Product"] = "坤灵网游";
            newRow["Version"] = "1.1";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "坤灵网游";
            newRow["Version"] = "1.1";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);
            newRow = dt.NewRow();
            newRow["Product"] = "坤灵网游";
            newRow["Version"] = "1.1";
            newRow["Description"] = "比大话更幼稚";
            dt.Rows.Add(newRow);

string path = System.IO.Path.Combine(Application.StartupPath, "test01.xls");
            if (File.Exists(path))
            {
                File.Delete(path);
            }

Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

Cells cells = worksheet.Cells;

//先设置标题项目:如大修件,日常备件等

cells.Merge(1, 0, 1, 4);

cells[1, 0].PutValue("XX月份供应商绩效考核报表");
            cells[1, 0].SetStyle(set_title_style(workbook,TextAlignmentType.Center));
            cells.Merge(2, 0, 1,3);
            cells[2, 0].PutValue("供应商代码:    E001");
            cells.Merge(3, 0, 1, 3);
            cells[3, 0].PutValue("供应商名称:    艾睿电子(深圳)有限公司");
            cells.Merge(4, 0, 1, 3);
            cells[4, 0].PutValue("供应商性质:    代理商");
            cells.Merge(5, 0, 1, 2);
            cells[5, 0].PutValue("总分:      ");
            cells.Merge(5, 2, 1, 2);
            cells[5, 2].PutValue("考核等级:     ");

string strCell1 = "";
            string strCell2 = "";
            for (int i = 0; i < dt.Rows.Count;i++)
            {
                if (strCell1 != dt.Rows[i][0].ToString())
                {
                    strCell1 = dt.Rows[i][0].ToString();
                    int number = 0;
                    for (int j = i; j < dt.Rows.Count; j++)
                    {
                        if (strCell1 == dt.Rows[j][0].ToString())
                        {
                            number++;
                        }
                        else
                        {
                            break;
                        }

}

cells.Merge(i + 6, 0, number, 1);
                    cells[i + 6, 0].PutValue(dt.Rows[i][0]);
                }

if (strCell2 != dt.Rows[i][1].ToString())
                {
                    strCell2 = dt.Rows[i][1].ToString();
                    int number = 0;
                    for (int j = i; j < dt.Rows.Count; j++)
                    {
                        if (strCell2 == dt.Rows[j][1].ToString())
                        {
                            number++;
                        }
                        else
                        {
                            break;
                        }

}

cells.Merge(i + 6, 1, number, 1);
                    cells[i + 6, 1].PutValue(dt.Rows[i][1]);
                }
 
                cells[i + 6, 2].PutValue(dt.Rows[i][2]);    
            }

Cells cells2 = worksheet.Cells;
            int columnCount = cells2.MaxColumn;  //获取表页的最大列数
            int rowCount = cells.MaxRow;        //获取表页的最大行数

for (int col = 0; col < columnCount; col++)
            {
                worksheet.AutoFitColumn(col, 0, rowCount);
            }

cells2.SetColumnWidth(0, 15);
                cells2.SetColumnWidth(1, 15);
                cells2.SetColumnWidth(2, 30);
                cells2.SetColumnWidth(3, 4);
                cells2.SetColumnWidth(4, 4);
            
            for (int col = 0; col < rowCount; col++)
            {
                cells2.SetRowHeight(col+1, 26);
            }

workbook.Save(System.IO.Path.Combine(Application.StartupPath, "test01.xls"));
            workbook.Save(System.IO.Path.Combine(Application.StartupPath, "test01.pdf"), SaveFormat.Pdf);

}

//一般标题样式
        protected Aspose.Cells.Style get_title_style(Workbook workbook, Color clrmy)
        {
            Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];
            styleTitle.HorizontalAlignment = TextAlignmentType.Center;  //标题居中对齐
            styleTitle.VerticalAlignment = TextAlignmentType.Bottom;    //垂直底对齐       
            styleTitle.Font.Name = "Arial";     //字体
            styleTitle.Font.Size = 11;          //字体大小
            styleTitle.IsTextWrapped = true;    //自动换行
            styleTitle.Font.Color = clrmy;
            return styleTitle;
        }
        //------------------------------------------------------------------------
        // 工作表标题行,第一行样式
        //------------------------------------------------------------------------
        protected Aspose.Cells.Style set_title_style(Workbook workbook, TextAlignmentType aliCenter)
        {
            Aspose.Cells.Style style_top = workbook.Styles[workbook.Styles.Add()];
            style_top.HorizontalAlignment = aliCenter;                  //标题居中对齐
            style_top.Font.Size = 18;                                   //字体大小
            style_top.Font.Color = System.Drawing.Color.Blue;
            style_top.Font.IsBold = true;
            return style_top;
        }
 
    }
}

Aspose.Cells 对excel的使用总结的更多相关文章

  1. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

  2. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

  3. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  4. 怎么使用Aspose.Cells读取excel 转化为Datatable

    说明:vs2012 asp.net mvc4 c# 使用Aspose.Cells 读取Excel 转化为Datatable 1.HTML前端代码 <%@ Page Language=" ...

  5. 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称

    说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...

  6. Aspose.cells 读取Excel表中的图片问题

    一.说明 本文主要是讲解,怎么使用aspose.cells读取Excel表中的图片,并把图片转换成流或是image对象. 二.开发环境说明 开发工具vs2012,c#语言, 三.Aspose.cell ...

  7. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  8. Aspose.Cells导出Excel(2)

    DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...

  9. 【转】Aspose.Cells读取excel文件

    Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...

  10. aspose.Cells 导出Excel

    aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...

随机推荐

  1. [转]解读Unity中的CG编写Shader系列6——不透明度与混合

    1.不透明度当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段着色器以及后面的环节的主要工作是输出颜色与深度到帧缓存中,所以两个纹理在每个像素上的颜色到 ...

  2. jquery源码解析:jQuery工具方法Callbacks详解

    我们首先来讲下Callbacks是如何使用的:第一个例子 function a(){} function b(){} var cb = $.Callbacks(); cb.add(a); cb.add ...

  3. Event(补交作业)

    三种方法可以创建Eventhandler 1.

  4. 搭建maven环境——nexus

    第一步:部署maven环境和jdk环境 jdk直接用openjdk1. :yum -y install java--openjdk maven环境 wget http://mirror.rise.ph ...

  5. L1-1 天梯赛座位分配

    天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] 支队伍,每队 10 位 ...

  6. bzoj 3261 最大异或和 可持久化字典树(01树)

    题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...

  7. Oracle 行列转换函数pivot、unpivot的使用(二)

    一.行转列pivot 关键函数pivot,其用法如下 pivot(聚合函数 for 列名 in(类型)) select * from table_name pivot(max(column_name) ...

  8. 人机交互print

    python两种输出类型: 1· 表达式语句,python理解的 #  repr() 2· print() 结果是给用户看的      # str()  >>> "Hell ...

  9. Git版本回退和撤销修改

    版本回退: 在实际工作中,我们会不断对文件进行修改,然后不断提交修改到版本库里,一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失. ...

  10. 2019年UX设计新趋势

    UX设计总是在不断变化中.最近短短两年的时间里,我们已经看到,很多地方都大规模采用颠覆性技术,比如语音用户界面,混合现实和智能家居设备.设计这些体验的实际过程可能保持不变,但新技术的出现引发了新的行为 ...