123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using DevExpress.XtraEditors;
- using System.IO;
- using DevExpress.Spreadsheet;
- using ProjectBase.Data.Logs;
- namespace ProjectBase.Controls.DocViewer
- {
- /// <summary>
- /// Excel控件的测试例子
- /// </summary>
- public partial class FrmExcelView : DevExpress.XtraEditors.XtraForm
- {
- /// <summary>
- /// 加载流数据
- /// </summary>
- public Stream Stream { get; set; }
- /// <summary>
- /// 文件后缀名,如.xls
- /// </summary>
- public string Extension { get; set; }
- /// <summary>
- /// 文档文件路径。如果指定了该属性,可以不用设置Stream和Extension属性。
- /// </summary>
- public string FilePath { get; set; }
- /// <summary>
- /// 是否显示打开文件按钮
- /// </summary>
- public bool ShowBtnOpenFile
- {
- get { return m_ShowBtnOpenFile; }
- set
- {
- m_ShowBtnOpenFile = value;
- this.btnOpenFile.Visible = this.ShowBtnOpenFile;
- }
- }
- //是否显示保存文件按钮
- public bool ShowBtnSaveFile
- {
- get { return m_ShowBtnSaveFile; }
- set
- {
- m_ShowBtnSaveFile = value;
- this.btnSaveFile.Visible = this.ShowBtnSaveFile;
- }
- }
- //记录窗体的名称
- readonly string mainFormText;
- private bool m_ShowBtnOpenFile = false;//是否显示打开文件按钮
- private bool m_ShowBtnSaveFile = false;//是否显示保存文件按钮
- public FrmExcelView()
- {
- InitializeComponent();
- //记录窗体的名称,并实现文档变化事件的处理,方便显示新的文件名称
- mainFormText = this.Text;
- this.spreadsheetControl1.DocumentLoaded += new EventHandler(spreadsheetControl1_DocumentLoaded);
- }
- private void FrmExcelView_Load(object sender, EventArgs e)
- {
- //如果文件流不为空,首先根据Stream对象加载文档,否则根据文件路径进行加载
- if (!this.DesignMode)
- {
- if (this.Stream != null)
- {
- #region MyRegion
- try
- {
- if (!string.IsNullOrEmpty(Extension))
- {
- if (Extension.Equals(".xls", StringComparison.OrdinalIgnoreCase))
- {
- this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
- }
- else if (Extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
- {
- this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xlsx);
- }
- else if (Extension.Equals(".csv", StringComparison.OrdinalIgnoreCase))
- {
- this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Csv);
- }
- else
- {
- this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
- }
- }
- else
- {
- this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
- }
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("加载Excel文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("加载Excel文件失败,错误为{0}", ex.ToString()));
- }
- #endregion
- }
- else if (!string.IsNullOrEmpty(FilePath))
- {
- this.spreadsheetControl1.LoadDocument(this.FilePath);
- }
- }
- }
- /// <summary>
- /// 文档变化后,实现对新文件名称的显示
- /// </summary>
- void spreadsheetControl1_DocumentLoaded(object sender, EventArgs e)
- {
- string fileName = Path.GetFileName(this.spreadsheetControl1.Document.Path);
- if (String.IsNullOrEmpty(fileName))
- {
- Text = mainFormText;
- }
- else
- {
- Text = fileName + " - " + mainFormText;
- }
- }
- /// <summary>
- /// 打开Excel文件
- /// </summary>
- private void btnOpenFile_Click(object sender, EventArgs e)
- {
- string filePath;
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.Filter = "Excel(*.xls)|*.xls|All File(*.*)|*.*";
- dialog.Title = "Excel选择";
- dialog.RestoreDirectory = true;
- dialog.FileName = null;
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- filePath = dialog.FileName;
- }
- else
- {
- filePath = string.Empty;
- }
- if (!string.IsNullOrEmpty(filePath))
- {
- IWorkbook workbook = spreadsheetControl1.Document;
- workbook.LoadDocument(filePath);
- }
- }
- /// <summary>
- /// 保存Excel文件
- /// </summary>
- private void btnSaveFile_Click(object sender, EventArgs e)
- {
- spreadsheetControl1.SaveDocument();
- this.Close();
- }
- /// <summary>
- /// 另存为Excel文件
- /// </summary>
- private void btnSaveAs_Click(object sender, EventArgs e)
- {
- string dir = System.Environment.CurrentDirectory;
- string filePath = null;
- SaveFileDialog dialog = new SaveFileDialog();
- dialog.Filter = "Excel(*.xls)|*.xls|All File(*.*)|*.*";
- dialog.Title = "保存Excel";
- dialog.FileName = "";
- dialog.RestoreDirectory = true;
- if (!string.IsNullOrEmpty(dir))
- {
- dialog.InitialDirectory = dir;
- }
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- filePath = dialog.FileName;
- }
- if (!string.IsNullOrEmpty(filePath))
- {
- try
- {
- IWorkbook workbook = spreadsheetControl1.Document;
- workbook.SaveDocument(filePath);
- DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- this.Close();
- }
- catch (Exception ex)
- {
- DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LogHelper.log.Error(string.Format("保存Excel文件失败,错误为{0}", ex.ToString()));
- }
- }
- }
- /// <summary>
- /// Excel文件打印
- /// </summary>
- private void btnPreview_Click(object sender, EventArgs e)
- {
- this.Close();
- this.spreadsheetControl1.ShowPrintPreview();
- }
- private void btnClose_Click(object sender, EventArgs e)
- {
- spreadsheetControl1.SaveDocument();
- this.Close();
- }
- }
- }
|