c#word或excel怎么读取和导出

用winform做了一个小软件,希望成批量的导入信息,想用excel来导入信息,怎么做?其次当里面有大量的信息的时候,我改如何将其信息导出一个word文件中后进行编辑?
2026年09月21日 17:46
有2个网友回答
网友(1):

不会导出word
EXCLE导入用WINFORM中的DATAGRIDVIEW来显示
参考下下面的代码
try
{
OpenFileDialog openfile = new OpenFileDialog(); openfile.DefaultExt = "xlsx";
openfile.Filter = "Excel文件|*.xlsx";
if (openfile.ShowDialog() == DialogResult.OK)
{
string s;
s = openfile.FileName.ToString();
string strCon = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + s + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
//上面为offic2007或2010 下面为2003
//string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + s + ";Extended properties=Excel 8.0";
OleDbConnection myconn = new OleDbConnection(strCon);
string strcom = "select * from [Sheet1$]";
myconn.Open();
OleDbCommand comm = new OleDbCommand(strcom, myconn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = comm; myconn.Close();
DataSet ds1 = new DataSet();
da.Fill(ds1, "[Sheet1$]");
dataGridView1.DataSource = ds1.Tables[0];
label11.BackColor = Color.Green;
label11.Text = "上传EXCEL档以及预览成功并可以修改其中的值";
}
}
catch (Exception EX)
{
label11.BackColor = Color.Red;
label11.Text = EX.ToString();
}
就可以显示在datagirdview上面了
导入数据库
try
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("192.168.0.210", "test", "root", "sugou").AsString);
conn.Open();
MySQLCommand comm = new MySQLCommand("set names gb2312", conn);
comm.ExecuteNonQuery();
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
string a1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
string a2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
string a3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
string a4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
string a5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
string a6 = dataGridView1.Rows[i].Cells[5].Value.ToString();
string a7 = dataGridView1.Rows[i].Cells[6].Value.ToString();
string sql = "insert into st_category(cat_one,cat_two,cat_three,cat_four,cat_five,cat_six,cat_seven) values('" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" + a5 + "','" + a6 + "','" + a7 + "')";

MySQLCommand comn = new MySQLCommand(sql, conn);
comn.ExecuteNonQuery();
}
}
catch (Exception ex)
{
label11.Text = ex.ToString();
label11.BackColor = Color.Red;
}
finally
{

}
导出EXCEL
写不完了

网友(2):

用PageOffice很简单就能实现的。