Aspose.Cells处理EXCEL数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspose.Cells;

namespace AsposeTest
{
    public partial class ExcelForm : Form
    {
        public ExcelForm()
        {
            InitializeComponent();
        }
        private string InputFile = "";
        private string OutputFile = "";
        private Workbook book = null;
        private Worksheet sheet = null;
        private Workbook inbook = null;
        private Worksheet insheet = null;

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                tbInputFile.Text = dlg.FileName;
                InputFile = dlg.FileName;
            }
        }
        private DataTable ProcessInputFile(string file)
        {
            Workbook book = new Workbook();
            book.Open(InputFile);
            Worksheet sheet = book.Worksheets[0];
            Cells cells = sheet.Cells;
            //获取excel中的数据保存到一个datatable中   
            DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
            // dt_Import. 
            
            
            return dt_Import;  

        }
        private void btnOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";
            saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CreatePrompt = true;
            saveFileDialog1.Title = "导出文件保存路径";
            //saveFileDialog1.ShowDialog();   
            //string strName = saveFileDialog1.FileName;   
            //设置默认文件类型显示顺序   
            //saveFileDialog1.FilterIndex = 2;   
            //保存对话框是否记忆上次打开的目录   
            saveFileDialog1.RestoreDirectory = true;  

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                OutputFile = saveFileDialog1.FileName;
                ProcessInputFile(InputFile, OutputFile);
            }

        }
        private void ProcessInputFile(string inputFile, string outputFile)
        {
            book = new Workbook();  //新建Excel

            sheet = book.Worksheets[0]; //新建sheet
            bool OK_NO = DatatableToExcel("");
            if (OK_NO)
            {
                MessageBox.Show("导出成功", "*^_^* 温馨提示信息", MessageBoxButtons.OK);
            }
            else
            {
            }  
        }
        //导出------------下一篇会用到这个方法   
        public Boolean DatatableToExcel(string data)
        {
            Boolean yn = false;
            try
            {
                sheet.Name = "测试";
                sheet.Cells.Merge(0, 0, 1, 5);
                sheet.Cells.Merge(1, 0, 1, 5);
                Cell cell1 = sheet.Cells[0, 0];
                cell1.PutValue("标题是");
               // cell1.Style.HorizontalAlignment = TextAlignmentType.Center;
              //  cell1.Style.Font.Name = "黑体";
             //   cell1.Style.Font.Size = 14;
             //   cell1.Style.Font.IsBold = true;
                Cell cell2 = sheet.Cells[1, 0];
                cell2.PutValue("查询时间:" + DateTime.Now.ToLocalTime());
              //  cell2.SetStyle(Font);  
   
                Cell cell = null;
                for (int col = 0; col < 5; col++)
                {
                    cell = sheet.Cells[2, col];
                    cell.PutValue(string.Format("第{0}列",col));
               //     cell.SetStyle(.IsBold = true;
                }  
   
                for (int r = 0; r < 4; r++)
                {
                    for (int c = 0; c < 5; c++)
                    {
                        sheet.Cells[r + 3, c].PutValue((r * c).ToString());
                    }
                }

                sheet.AutoFitColumns();
                sheet.AutoFitRows();   
                book.Save(OutputFile);
                yn = true;
                return yn;
            }
            catch (Exception e)
            {
                return yn;

            }
        }
    }
}

http://www.cnblogs.com/wuhuacong/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值