您的位置:首页 > 编程语言 > ASP

Aspose.Cells处理EXCEL数据

2013-11-08 00:33 393 查看
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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: