110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
List<test> ts = new List<test>();
private void button1_Click(object sender, EventArgs e)
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
xmlResult.Append("<mytest>\r\n");
Random r = new Random();
DataGridView dg = dataGridView1;
for (int i = 0; i < dg.RowCount-1; i++) {
test t = new test();
t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
ts.Add(t);
}
string txt = "";
string sPath = "D:\\ces\\";//存放的文件夹路径
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);//不存在创建文件夹
}
int s = 0;
foreach(test t in ts){
xmlResult.AppendFormat("<ces id=\"{0}\" category=\"{1}\">\r\n", s, "第" + s + "行");
xmlResult.AppendFormat("<ces1>{0}</ces1>\r\n", t.Ces1);
xmlResult.AppendFormat("<ces2>{0}</ces2>\r\n", t.Ces2);
xmlResult.Append("</ces>\r\n");
//txt += t.Ces1 + " " + t.Ces2 + "\r\n";
s++;
}
xmlResult.Append("</mytest>\r\n");
string name = r.Next(000000000, 999999999).ToString() + ".xml";
// 创建文件
FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(xmlResult); // 写入文本
sw.Close(); //关闭文件
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
public class test {
string ces1;
public string Ces1
{
get { return ces1; }
set { ces1 = value; }
}
string ces2;
public string Ces2
{
get { return ces2; }
set { ces2 = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
List<test> ts = new List<test>();
private void button1_Click(object sender, EventArgs e)
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
xmlResult.Append("<mytest>\r\n");
Random r = new Random();
DataGridView dg = dataGridView1;
for (int i = 0; i < dg.RowCount-1; i++) {
test t = new test();
t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
ts.Add(t);
}
string txt = "";
string sPath = "D:\\ces\\";//存放的文件夹路径
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);//不存在创建文件夹
}
int s = 0;
foreach(test t in ts){
xmlResult.AppendFormat("<ces id=\"{0}\" category=\"{1}\">\r\n", s, "第" + s + "行");
xmlResult.AppendFormat("<ces1>{0}</title>\r\n", t.Ces1);
xmlResult.AppendFormat("<ces2>{0}</title>\r\n", t.Ces2);
xmlResult.Append("</ces>\r\n");
//txt += t.Ces1 + " " + t.Ces2 + "\r\n";
s++;
}
xmlResult.Append("</mytest>\r\n");
string name = r.Next(000000000, 999999999).ToString() + ".xml";
// 创建文件
FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(xmlResult); // 写入文本
sw.Close(); //关闭文件
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
public class test {
string ces1;
public string Ces1
{
get { return ces1; }
set { ces1 = value; }
}
string ces2;
public string Ces2
{
get { return ces2; }
set { ces2 = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
List<test> ts = new List<test>();
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
DataGridView dg = dataGridView1;
for (int i = 0; i < dg.RowCount-1; i++) {
test t = new test();
t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
ts.Add(t);
}
string txt = "";
string sPath = "D:\\ces\\";//存放的文件夹路径
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);//不存在创建文件夹
}
foreach(test t in ts){
txt += t.Ces1 + " " + t.Ces2 + "\r\n";
}
string name = r.Next(000000000, 999999999).ToString() + ".txt";
// 创建文件
FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(txt); // 写入文本
sw.Close(); //关闭文件
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
public class test {
string ces1;
public string Ces1
{
get { return ces1; }
set { ces1 = value; }
}
string ces2;
public string Ces2
{
get { return ces2; }
set { ces2 = value; }
}
}
}