用C#实现WORD文档导出 我想用Button控件来实现导出WORD文档,但是做不好,请高手指教!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有程序也发我下,谢谢谢谢。[email protected] 导出什么?是数据库信息导出写入到Word中么,那么用书签即可。 #region 保存到Word private void SaveSelectResultToWord(string strTableName) { if (SelectResultDataTable.Rows.Count < 1) { MessageBox.Show("无可用于打印的数据。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string strTableKey = dataGridView7[0, dataGridView7.CurrentCell.RowIndex].Value.ToString(); string strFieldName = comboBox4.Items[0].ToString(); string strSelectCommandText = "select * from " + strTableName + " where " + strFieldName + " = '" + strTableKey + "'"; DataTable dt = dbManageClass.SelectTable(strSelectCommandText, strTableName).Tables[strTableName]; if (dt.Rows.Count == 1) { object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; string strAppPath = Environment.CurrentDirectory; strAppPath = ReturnPath(strAppPath, 2); object oTemplate = strAppPath + @"\Cyclostyle\" + strTableName + ".dot"; oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing); for (int i = 0; i < dt.Columns.Count; i++) { object oBookMark = dt.Columns[i].Caption; oDoc.Books.get_Item(ref oBookMark).Range.Text = dt.Rows[0][i].ToString(); } } else { MessageBox.Show("查询的结果无效。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Information); } } #endregion 如果是数据库中保存的是Word文档#region 获得Oracle数据库中Blob字段的方法 /// <summary> /// 获得Oracle数据库中Blob字段的方法,用于下载文档等 /// </summary> /// <param name="_SaveFileName">保存的文件名</param> /// <param name="_SelectCommandText">获得BLOB字段的查询命令</param> /// <param name="_ColumnsIndex">BLOB字段在记录集中的列索引</param> /// <param name="_TableName">查询的表名称</param> /// <returns>包含错误信息的文本</returns> public string GetOracleBlobToWord(string _SaveFileName, string _SelectCommandText, int _ColumnsIndex, string _TableName) { string _ReturnText = string.Empty; //if (System.IO.File.Exists(_SaveFileName)) //{ // _ReturnText = "指定的文件不存在。"; // return _ReturnText; //} System.IO.FileStream fileStream = new System.IO.FileStream(_SaveFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(fileStream); //Byte[] byteBLOBData = GetOracleBlob(_SelectCommandText, _TableName, blobColumnsIndex); Byte[] byteBLOBData = new Byte[0]; DataSet ds = clientBase.SelectOracle(_SelectCommandText, _TableName); DataTable dt = ds.Tables[0]; if (dt.Rows.Count == 0) { _ReturnText = "没有发现可用的记录"; return _ReturnText; } if (dt.Columns[_ColumnsIndex].DataType.ToString() != "System.Byte[]") { _ReturnText = "没有发现可用的字段"; return _ReturnText; } //需要判断文档字段为空的情况------------------------------- try { byteBLOBData = (Byte[])(dt.Rows[0][_ColumnsIndex]); } catch (Exception) { fileStream.Close(); _ReturnText = "对不起,当前选定的记录没有发现关联文档。"; return _ReturnText; } //if (dt.Rows[0][_ColumnsIndex] == null) //{ // _ReturnText = "没有数据"; // return _ReturnText; //} //byteBLOBData = (Byte[])(dt.Rows[0][_ColumnsIndex]); if (byteBLOBData == null) { _ReturnText = "没有数据"; return _ReturnText; } binaryWriter.Write(byteBLOBData); binaryWriter.Flush(); fileStream.Flush(); binaryWriter.Close(); fileStream.Close(); return _ReturnText; } 是TextBox控件上的TEXT,我想加个button按钮导出textbox的内容到word。比如说我现在有TextBox1,TextBox2,TextBox3 上面都是有文字的,现在我想在word里显示“姓名“+textbox1.text+”学号”+textbox2.text+"性别“textbox3.text .net操作WORD的读取和写入http://zhidao.baidu.com/question/18038744.html 数据库中不应保存word文档及其他二进制文件.这是数据库开发的一个原则.考虑到用户的office版本不同,你应该考虑使用rtf格式来兼容各版本office关于rtf格式,这个在网上资源很多,我就不转载了. 菜鸟问题 WebBrowser 自动分页打印 表格的边框被分开了 50分求助 改变窗口大小时的重绘问题 100分 求解决思路 能具体点更好 请教:窗口显示问题 获取datagrid 或 gridview 列的值 求visual studio .net 2003下好用的表格控件(Form开发) 100分求FolderBrowserDialog问题 菜鸟发问-----datagrid选行问题!!!!!!! (RMB)求一个解析验证码程序或代码 怎么让程序抛了异常就不执行下面代码。 急等 大家有没用C#写过编译器
[email protected]
是数据库信息导出写入到Word中么,那么用书签即可。
#region 保存到Word
private void SaveSelectResultToWord(string strTableName)
{
if (SelectResultDataTable.Rows.Count < 1)
{
MessageBox.Show("无可用于打印的数据。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string strTableKey = dataGridView7[0, dataGridView7.CurrentCell.RowIndex].Value.ToString();
string strFieldName = comboBox4.Items[0].ToString();
string strSelectCommandText = "select * from " + strTableName + " where " + strFieldName + " = '" + strTableKey + "'";
DataTable dt = dbManageClass.SelectTable(strSelectCommandText, strTableName).Tables[strTableName]; if (dt.Rows.Count == 1)
{
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true; string strAppPath = Environment.CurrentDirectory;
strAppPath = ReturnPath(strAppPath, 2);
object oTemplate = strAppPath + @"\Cyclostyle\" + strTableName + ".dot";
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
for (int i = 0; i < dt.Columns.Count; i++)
{
object oBookMark = dt.Columns[i].Caption;
oDoc.Books.get_Item(ref oBookMark).Range.Text = dt.Rows[0][i].ToString();
}
}
else
{
MessageBox.Show("查询的结果无效。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
/// <summary>
/// 获得Oracle数据库中Blob字段的方法,用于下载文档等
/// </summary>
/// <param name="_SaveFileName">保存的文件名</param>
/// <param name="_SelectCommandText">获得BLOB字段的查询命令</param>
/// <param name="_ColumnsIndex">BLOB字段在记录集中的列索引</param>
/// <param name="_TableName">查询的表名称</param>
/// <returns>包含错误信息的文本</returns>
public string GetOracleBlobToWord(string _SaveFileName, string _SelectCommandText, int _ColumnsIndex, string _TableName)
{
string _ReturnText = string.Empty; //if (System.IO.File.Exists(_SaveFileName))
//{
// _ReturnText = "指定的文件不存在。";
// return _ReturnText;
//} System.IO.FileStream fileStream = new System.IO.FileStream(_SaveFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(fileStream); //Byte[] byteBLOBData = GetOracleBlob(_SelectCommandText, _TableName, blobColumnsIndex);
Byte[] byteBLOBData = new Byte[0]; DataSet ds = clientBase.SelectOracle(_SelectCommandText, _TableName);
DataTable dt = ds.Tables[0]; if (dt.Rows.Count == 0)
{
_ReturnText = "没有发现可用的记录";
return _ReturnText;
} if (dt.Columns[_ColumnsIndex].DataType.ToString() != "System.Byte[]")
{
_ReturnText = "没有发现可用的字段";
return _ReturnText;
}
//需要判断文档字段为空的情况------------------------------- try
{
byteBLOBData = (Byte[])(dt.Rows[0][_ColumnsIndex]);
}
catch (Exception)
{
fileStream.Close();
_ReturnText = "对不起,当前选定的记录没有发现关联文档。";
return _ReturnText;
} //if (dt.Rows[0][_ColumnsIndex] == null)
//{
// _ReturnText = "没有数据";
// return _ReturnText;
//}
//byteBLOBData = (Byte[])(dt.Rows[0][_ColumnsIndex]);
if (byteBLOBData == null)
{
_ReturnText = "没有数据";
return _ReturnText;
}
binaryWriter.Write(byteBLOBData); binaryWriter.Flush();
fileStream.Flush();
binaryWriter.Close();
fileStream.Close(); return _ReturnText;
}
比如说我现在有TextBox1,TextBox2,TextBox3 上面都是有文字的,
现在我想在word里显示“姓名“+textbox1.text+”学号”+textbox2.text+"性别“textbox3.text
http://zhidao.baidu.com/question/18038744.html
关于rtf格式,这个在网上资源很多,我就不转载了.