FrmExcelView.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using System.IO;
  12. using DevExpress.Spreadsheet;
  13. using ProjectBase.Data.Logs;
  14. namespace ProjectBase.Controls.DocViewer
  15. {
  16. /// <summary>
  17. /// Excel控件的测试例子
  18. /// </summary>
  19. public partial class FrmExcelView : DevExpress.XtraEditors.XtraForm
  20. {
  21. /// <summary>
  22. /// 加载流数据
  23. /// </summary>
  24. public Stream Stream { get; set; }
  25. /// <summary>
  26. /// 文件后缀名,如.xls
  27. /// </summary>
  28. public string Extension { get; set; }
  29. /// <summary>
  30. /// 文档文件路径。如果指定了该属性,可以不用设置Stream和Extension属性。
  31. /// </summary>
  32. public string FilePath { get; set; }
  33. /// <summary>
  34. /// 是否显示打开文件按钮
  35. /// </summary>
  36. public bool ShowBtnOpenFile
  37. {
  38. get { return m_ShowBtnOpenFile; }
  39. set
  40. {
  41. m_ShowBtnOpenFile = value;
  42. this.btnOpenFile.Visible = this.ShowBtnOpenFile;
  43. }
  44. }
  45. //是否显示保存文件按钮
  46. public bool ShowBtnSaveFile
  47. {
  48. get { return m_ShowBtnSaveFile; }
  49. set
  50. {
  51. m_ShowBtnSaveFile = value;
  52. this.btnSaveFile.Visible = this.ShowBtnSaveFile;
  53. }
  54. }
  55. //记录窗体的名称
  56. readonly string mainFormText;
  57. private bool m_ShowBtnOpenFile = false;//是否显示打开文件按钮
  58. private bool m_ShowBtnSaveFile = false;//是否显示保存文件按钮
  59. public FrmExcelView()
  60. {
  61. InitializeComponent();
  62. //记录窗体的名称,并实现文档变化事件的处理,方便显示新的文件名称
  63. mainFormText = this.Text;
  64. this.spreadsheetControl1.DocumentLoaded += new EventHandler(spreadsheetControl1_DocumentLoaded);
  65. }
  66. private void FrmExcelView_Load(object sender, EventArgs e)
  67. {
  68. //如果文件流不为空,首先根据Stream对象加载文档,否则根据文件路径进行加载
  69. if (!this.DesignMode)
  70. {
  71. if (this.Stream != null)
  72. {
  73. #region MyRegion
  74. try
  75. {
  76. if (!string.IsNullOrEmpty(Extension))
  77. {
  78. if (Extension.Equals(".xls", StringComparison.OrdinalIgnoreCase))
  79. {
  80. this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
  81. }
  82. else if (Extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
  83. {
  84. this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xlsx);
  85. }
  86. else if (Extension.Equals(".csv", StringComparison.OrdinalIgnoreCase))
  87. {
  88. this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Csv);
  89. }
  90. else
  91. {
  92. this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
  93. }
  94. }
  95. else
  96. {
  97. this.spreadsheetControl1.LoadDocument(this.Stream, DocumentFormat.Xls);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. DevExpress.XtraEditors.XtraMessageBox.Show("加载Excel文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  103. LogHelper.log.Error(string.Format("加载Excel文件失败,错误为{0}", ex.ToString()));
  104. }
  105. #endregion
  106. }
  107. else if (!string.IsNullOrEmpty(FilePath))
  108. {
  109. this.spreadsheetControl1.LoadDocument(this.FilePath);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 文档变化后,实现对新文件名称的显示
  115. /// </summary>
  116. void spreadsheetControl1_DocumentLoaded(object sender, EventArgs e)
  117. {
  118. string fileName = Path.GetFileName(this.spreadsheetControl1.Document.Path);
  119. if (String.IsNullOrEmpty(fileName))
  120. {
  121. Text = mainFormText;
  122. }
  123. else
  124. {
  125. Text = fileName + " - " + mainFormText;
  126. }
  127. }
  128. /// <summary>
  129. /// 打开Excel文件
  130. /// </summary>
  131. private void btnOpenFile_Click(object sender, EventArgs e)
  132. {
  133. string filePath;
  134. OpenFileDialog dialog = new OpenFileDialog();
  135. dialog.Filter = "Excel(*.xls)|*.xls|All File(*.*)|*.*";
  136. dialog.Title = "Excel选择";
  137. dialog.RestoreDirectory = true;
  138. dialog.FileName = null;
  139. if (dialog.ShowDialog() == DialogResult.OK)
  140. {
  141. filePath = dialog.FileName;
  142. }
  143. else
  144. {
  145. filePath = string.Empty;
  146. }
  147. if (!string.IsNullOrEmpty(filePath))
  148. {
  149. IWorkbook workbook = spreadsheetControl1.Document;
  150. workbook.LoadDocument(filePath);
  151. }
  152. }
  153. /// <summary>
  154. /// 保存Excel文件
  155. /// </summary>
  156. private void btnSaveFile_Click(object sender, EventArgs e)
  157. {
  158. spreadsheetControl1.SaveDocument();
  159. this.Close();
  160. }
  161. /// <summary>
  162. /// 另存为Excel文件
  163. /// </summary>
  164. private void btnSaveAs_Click(object sender, EventArgs e)
  165. {
  166. string dir = System.Environment.CurrentDirectory;
  167. string filePath = null;
  168. SaveFileDialog dialog = new SaveFileDialog();
  169. dialog.Filter = "Excel(*.xls)|*.xls|All File(*.*)|*.*";
  170. dialog.Title = "保存Excel";
  171. dialog.FileName = "";
  172. dialog.RestoreDirectory = true;
  173. if (!string.IsNullOrEmpty(dir))
  174. {
  175. dialog.InitialDirectory = dir;
  176. }
  177. if (dialog.ShowDialog() == DialogResult.OK)
  178. {
  179. filePath = dialog.FileName;
  180. }
  181. if (!string.IsNullOrEmpty(filePath))
  182. {
  183. try
  184. {
  185. IWorkbook workbook = spreadsheetControl1.Document;
  186. workbook.SaveDocument(filePath);
  187. DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. this.Close();
  189. }
  190. catch (Exception ex)
  191. {
  192. DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  193. LogHelper.log.Error(string.Format("保存Excel文件失败,错误为{0}", ex.ToString()));
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// Excel文件打印
  199. /// </summary>
  200. private void btnPreview_Click(object sender, EventArgs e)
  201. {
  202. this.Close();
  203. this.spreadsheetControl1.ShowPrintPreview();
  204. }
  205. private void btnClose_Click(object sender, EventArgs e)
  206. {
  207. spreadsheetControl1.SaveDocument();
  208. this.Close();
  209. }
  210. }
  211. }