winform程序中关于sql数据库数据导出到word的问题....
答案:1 悬赏:40 手机版
解决时间 2021-01-14 22:43
- 提问者网友:孤凫
- 2021-01-14 10:38
winform程序中关于sql数据库数据导出到word的问题....
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-01-14 10:49
///
/// 导出到Word
///
/// 导出的数据DataTable
/// 是否显示列名
public static void OutPutWordDT(DataTable dt, bool isColname)
{
Object Nothing = System.Reflection.Missing.Value;
Word.Application oword = new Word.Application();//word Application
Word.Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//文档
odoc.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
try
{
//在word以表格形式存储数据
Word.Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing);
otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式
otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框样式
if (isColname)//列名称
{
int intcol = 0;
for (int ii = 0; ii < dt.Columns.Count; ii++)
{
intcol += 1;
otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName;
otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
//写表格内容
int intRow = 1;
for (int ii = 0; ii < dt.Rows.Count; ii++)
{
intRow += 1;
int intCol = 0;
for (int jj = 0; jj < dt.Columns.Count; jj++)
{
intCol += 1;
otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString();
otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
oword.Visible = true;
}
catch (Exception) { }
finally
{
System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
for (int i = 0; i < CurrentProcess.Length; i++)
{
if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
{
try
{
CurrentProcess[i].Kill();
}
catch
{
}
}
}
}
}
----------------------------------------------------
用代码对word操作时myWordDoc.Paragraphs.Last.Range.Text =该怎样用这个字符数组对其赋值?
既然是字符数组,那就这样:
假设你得到的字符数组为stringArray[ ]
try
{
foreach(string str in stringArray)
{
myWordDoc.Paragraphs.Last.Range.Text =str;//赋值
}
}
catch(Exception ex
{
MessageBox.Show(ex.Message);
}
或者可以用FOR循环
try
{
for(int i=0;i< stringArray.Length;i++)
{
myWordDoc.Paragraphs.Last.Range.Text =stringArray[i];//赋值
}
}
catch(Exception ex
{
MessageBox.Show(ex.Message);
}追问谢谢你的回答,可是我从数据库读出来的不是表格。我现在做的是个关于题库的管理系统,从数据库读出来的是一个个的题目到word文档显示。我没说清出,使用字节数组byte【】读出来的。刚咨询了老师,老师说用文件插入的方式,将数据放到新建的word文档中。正在试。不知道你有没有更简单的方法...
/// 导出到Word
///
/// 导出的数据DataTable
/// 是否显示列名
public static void OutPutWordDT(DataTable dt, bool isColname)
{
Object Nothing = System.Reflection.Missing.Value;
Word.Application oword = new Word.Application();//word Application
Word.Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//文档
odoc.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
try
{
//在word以表格形式存储数据
Word.Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing);
otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式
otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框样式
if (isColname)//列名称
{
int intcol = 0;
for (int ii = 0; ii < dt.Columns.Count; ii++)
{
intcol += 1;
otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName;
otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
//写表格内容
int intRow = 1;
for (int ii = 0; ii < dt.Rows.Count; ii++)
{
intRow += 1;
int intCol = 0;
for (int jj = 0; jj < dt.Columns.Count; jj++)
{
intCol += 1;
otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString();
otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
oword.Visible = true;
}
catch (Exception) { }
finally
{
System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
for (int i = 0; i < CurrentProcess.Length; i++)
{
if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
{
try
{
CurrentProcess[i].Kill();
}
catch
{
}
}
}
}
}
----------------------------------------------------
用代码对word操作时myWordDoc.Paragraphs.Last.Range.Text =该怎样用这个字符数组对其赋值?
既然是字符数组,那就这样:
假设你得到的字符数组为stringArray[ ]
try
{
foreach(string str in stringArray)
{
myWordDoc.Paragraphs.Last.Range.Text =str;//赋值
}
}
catch(Exception ex
{
MessageBox.Show(ex.Message);
}
或者可以用FOR循环
try
{
for(int i=0;i< stringArray.Length;i++)
{
myWordDoc.Paragraphs.Last.Range.Text =stringArray[i];//赋值
}
}
catch(Exception ex
{
MessageBox.Show(ex.Message);
}追问谢谢你的回答,可是我从数据库读出来的不是表格。我现在做的是个关于题库的管理系统,从数据库读出来的是一个个的题目到word文档显示。我没说清出,使用字节数组byte【】读出来的。刚咨询了老师,老师说用文件插入的方式,将数据放到新建的word文档中。正在试。不知道你有没有更简单的方法...
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯