**
这个软件可以快速生成多个Word/Excel/txt/jpg文件,并在每个文件中添加10位随机字符组成的字符串,这些字符串都是不重复的。
**
本程序中用到了Free Spire.Doc for .NET和Free Spire.Xls for .NET,下载安装后,将软件安装目录bin文件夹,选择你的NET版本,将其中的dll文件添加到c#项目的引用中。
下载链接:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html
界面:
自动添加文件,这是随机,也可以指定文件名:
添加各种文件的代码如下:
public void Specifiedname()
{
if (textBox1.Text != "" && textBox2.Text != "" && comboBox1.SelectedIndex != -1)
{
string path = System.IO.Path.Combine(textBox1.Text.ToString() + "\\" + textBox3.Text.ToString() + "1" + comboBox1.SelectedItem.ToString());
if (File.Exists(path)) //判断文件名存不存在
{
MessageBox.Show("文件名已存在,请重新输入!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
int a = int.Parse(textBox2.Text);
for (int i = 1; i <= a; i++)
{
if (comboBox1.SelectedItem.ToString() == ".txt")
{
{
string path1 = System.IO.Path.Combine(textBox1.Text.ToString(), textBox3.Text + i + comboBox1.SelectedItem.ToString());
File.WriteAllText(path1, randnum());
Thread.Sleep(100);
}
}
if (comboBox1.SelectedItem.ToString() == ".docx")
{
//创建一个Document实例
Document doc = new Document();
//添加一个section
Section sec = doc.AddSection();
//添加三个段落并写入文字
Paragraph para1 = sec.AddParagraph();
para1.AppendText(randnum());
//保存文档
string pa = textBox1.Text.ToString() + "\\";
doc.SaveToFile(pa + textBox3.Text + i + ".docx", Spire.Doc.FileFormat.Docx2013);
}
if (comboBox1.SelectedItem.ToString() == ".doc")
{
//创建一个Document实例
Document doc = new Document();
//添加一个section
Section sec = doc.AddSection();
//添加三个段落并写入文字
Paragraph para2 = sec.AddParagraph();
para2.AppendText(randnum());
//保存文档
string pa = textBox1.Text.ToString() + "\\";
doc.SaveToFile(pa + textBox3.Text + i + ".doc", Spire.Doc.FileFormat.Docx2013);
}
if (comboBox1.SelectedItem.ToString() == ".xlsx")
{
//新建Workbook
Workbook workbook = new Workbook();
//得到第一个Sheet页
Worksheet sheet = workbook.Worksheets[0];
//给A2这个单元格设置值
sheet.Range["A2"].Text = randnum();
//保存到物理路径
string pa = textBox1.Text.ToString() + "\\";
var strFullName = pa + textBox3.Text + i + ".xlsx";
workbook.SaveToFile(strFullName);
}
if (comboBox1.SelectedItem.ToString() == ".xls")
{
//新建Workbook
Workbook workbook = new Workbook();
//得到第一个Sheet页
Worksheet sheet = workbook.Worksheets[0];
//给A2这个单元格设置值
sheet.Range["A2"].Text = randnum();
//保存到物理路径
string pa = textBox1.Text.ToString() + "\\";
var strFullName = pa + textBox3.Text + i + ".xls";
workbook.SaveToFile(strFullName);
}
}
MessageBox.Show("已生成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("信息不完整,请将信息补充完整!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
不懂的地方欢迎提问。