1、成果展示
打开文件
页面设置按钮
里面的细节操作就不一一贴图了
2、代码展示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
删除ToolStripMenuItem.Enabled = false;
搜索ToolStripMenuItem.Enabled = false;
剪切ToolStripMenuItem.Enabled = false;
复制ToolStripMenuItem.Enabled = false;
}
string oldFilePath = Directory.GetCurrentDirectory();
string tempText = null;
/// <summary>
/// 反射,将实例对象与excel值进行链接,写入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///将每列的值与实例对象的属性建立连接
// List<PropertyAndColumn> propertyAndColumns = new List<PropertyAndColumn>();
// ///通过反射获取对象的值
// foreach (var property in sheetInfo.RowType.GetProperties())
// {
// if (property.GetCustomAttributes(typeof(ExcelColumnAttribute), true).SingleOrDefault() is ExcelColumnAttribute e)
// {
// if (e.WriteToExcel)
// {
// propertyAndColumns.Add(new PropertyAndColumn(property, e));
// }
//}
// }
// propertyAndColumns = propertyAndColumns.OrderBy(a => a.E.WriteOrder).ToList();
//for (int i = 0; i < propertyAndColumns.Count; i++)
//{
// var a = propertyAndColumns[i];
// row.CreateCell(i).SetCellValue(a.E.HeaderText);
// row.Height = 350;
// sheet.AutoSizeColumn(i);
//}
//导出Excel文件
private void button1_Click(object sender, EventArgs e)
{
IWorkbook wokbook = new XSSFWorkbook();
ISheet sheet = wokbook.CreateSheet(textBox1.Text);
IRow row = sheet.CreateRow(5);
ICell cell = row.CreateCell(0);
//如何往单元格填充数据
for (int i = 0; i < 5; i++)
{
cell.SetCellValue(i);
}
var lastIndex = sheet.LastRowNum;
for (int i = 0; i < lastIndex; i++)
{
var tempRow = sheet.GetRow(i);
var value = tempRow.GetCell(0).StringCellValue;
}
<summary>
///
/// 导出文件
///<summary>
string path = "";
if (!Directory.Exists(path))
{
MessageBox.Show("路径不存在,重新选择");
FolderBrowserDialog fb = new FolderBrowserDialog();
if (fb.ShowDialog() == DialogResult.OK)
{
path = fb.SelectedPath;
}
}
try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
if (path != null)
{
wokbook.Write(fs);
if (fs.CanRead)
{
fs.Flush();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void 新窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
在原窗口的基础上新增一个窗口
///
FormTool.CreateForm();
//判断是否输入数据并点击了保存?
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
///打开一个文件的状态下--新建,原文件直接保存退出,新建一个无标题TXT
///鼠标右键,直接新建一个无标题TXT
///
this.Close();
FormTool.CreateForm();
判断是否有打开的文件
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
//打开文件
FolderBrowserDialog fb = new FolderBrowserDialog();
if (fb.ShowDialog()==DialogResult.OK)
{
//判断打开的文件是否是TXT格式的:各种文件格式文件头占的字节不一样,譬如:jpg,2个字节;png,8个字节;gif,6个字节。
//你只需要改变读取的字节数,在判断就可以了。
bool flag = JudgeFileType.JudgeTxtType(fb.SelectedPath);
string filename = Path.GetFileName(fb.SelectedPath); //获取文件名
//打开的是txt文件
if (flag)
{
textBox1.Text = File.ReadAllText(filename);
}
else
{
MessageBox.Show("请先选中文件再打开!");
}
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
//直接保存在当前路径下
//如何保存文件呢
//先获取当前文件位置 directory.getcurrentdirectory()
//判断当前路径是是否存在
JudgeFileType.saveFile(oldFilePath, textBox1.Text);
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "新建文本.txt";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//获取新路径
string newPath=Directory.GetCurrentDirectory();
//保存
JudgeFileType.saveFile(newPath, textBox1.Text);
}
string s = saveFileDialog1.FileName; //保存新文件的路径及名称
//判断是否保存成功
if (Directory.Exists(s))
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!请重试....");
}
}
private void 选项ToolStripMenuItem_Click(object sender, EventArgs e)
{
setupForm setupForm = new setupForm();
setupForm.Show();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
//有点麻烦
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Undo();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
//如何判断选中的内容:
//选中文本
tempText=textBox1.SelectedText;
Clipboard.SetDataObject(tempText);
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Focus();
tempText = textBox1.SelectedText;
//剪切数据
textBox1.Cut();
//将数据复制到系统剪切板
//Clipboard.SetDataObject(tempText);
//int startIndex=textBox1.SelectionStart;
//int length = textBox1.SelectedText.Length;
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!( Clipboard.ContainsText()))
{
MessageBox.Show("请先复制或剪切要粘贴的内容!");
return;
}
textBox1.Focus();
//获取插入位置
textBox1.SelectionStart = Convert.ToInt32(textBox1.SelectionStart.ToString());//Control.MousePosition;
//设置鼠标指定的位置粘贴:
textBox1.Paste();
}
/// <summary>
/// 删除功能:还需要在未选中文本情况下不可用---需优化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断是否选中文本,未选中,则控件不可使用
if (!string.IsNullOrEmpty(textBox1.SelectedText))
{
删除ToolStripMenuItem.Enabled = false;
}
tempText = textBox1.SelectedText;
textBox1.SelectedText = null;
}
private void 搜索ToolStripMenuItem_Click(object sender, EventArgs e)
{
//将选择的文本给百度
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//判断是否选中内容
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(textBox1.SelectedText))
{
删除ToolStripMenuItem.Enabled = true;
搜索ToolStripMenuItem.Enabled = true;
剪切ToolStripMenuItem.Enabled = true;
复制ToolStripMenuItem.Enabled = true;
}
else
{
删除ToolStripMenuItem.Enabled = false;
搜索ToolStripMenuItem.Enabled = false;
剪切ToolStripMenuItem.Enabled = false;
复制ToolStripMenuItem.Enabled = false;
}
}
}
}
3、难点
1、如何判断失去焦点,
2、如何两个事件之间的绑定
3、剪切功能、撤销操作,准备自己写方法去实现,结果textbox控件自带剪切、撤销的方法,想复杂了
4、删除、复制的按钮只有在选中文本时才是enable=true模式,其他时候都是不可编辑模式,当时想