在窗口应用程序中将数据表导出到ms字 [英] Export data table to ms word in window application
本文介绍了在窗口应用程序中将数据表导出到ms字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有数据表的窗口应用程序。数据来自sql server。
在这个数据表中,我有一些包含文本数据的列和包含图像路径的4列。图像存储在一个文件夹中。我想将数据表导出到带有图像的ms字。格式正确。
任何人都可以帮我解决这个问题。
我尝试了什么:
I have a window application with a data table. Data come from sql server.
In this data table i have some column with text data and 4 column with image path. Images store in a folder. I want to export the data table to ms word with image. In a proper format.
Can anyone help me how to do this.
What I have tried:
DataTable dt=new DataTable();
Spire.DataExport.RTF.RTFExport rtfExport = new Spire.DataExport.RTF.RTFExport();
rtfExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
rtfExport.DataTable = dt;
rtfExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
RTFStyle rtfStyle = new RTFStyle();
rtfStyle.FontColor = System.Drawing.Color.Blue;
rtfStyle.BackgroundColor = System.Drawing.Color.LightGreen;
rtfExport.RTFOptions.DataStyle = rtfStyle;
rtfExport.FileName = @"E\ToWord.doc";
rtfExport.SaveToFile();
以上代码将数据导出为ms字,但格式不正确,还有图像不出口。
帮助我如何做到这一点。
谢谢你
The above code export the data to ms word but not in proper format and also image not export.
help me how to do this .
Thank you
推荐答案
试试这个,
创建格式正确的HTML字符串,将html转换为ms word文档
http://stackoverflow.com/questions/5431580/convert-html-to-docx-in-c-sharp [ ^ ]
https:// weblogs.asp.net/dixin/convert-html-to-well-formatted-microsoft-word-document [ ^ ]
Try this,
create your well formatted HTML string, convert html to ms word document
http://stackoverflow.com/questions/5431580/convert-html-to-docx-in-c-sharp[^]
https://weblogs.asp.net/dixin/convert-html-to-well-formatted-microsoft-word-document[^]
try
{
progressBar1.Visible = true;
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
winword.Visible = false;
//Create a missing variable for missing value
object missing = System.Reflection.Missing.Value;
//Create a new document
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
document.Content.SetRange(0, 0);
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
object styleHeading1 = "Heading 1";
para1.Range.set_Style(ref styleHeading1);
para1.Range.Text = "Post Add report";
para1.Range.InsertParagraphAfter();
string samplePath2 = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "IPIMages" + Path.DirectorySeparatorChar;
string imagePath = samplePath2 + "logo.jpg";
Paragraph para = document.Content.Paragraphs.Add(ref missing);
para.Range.InsertParagraphBefore();
InlineShape map = document.InlineShapes.AddPicture(imagePath, ref missing, ref missing, ref missing);
map.Height = 176;
map.Width = 177;
para.Range.InsertParagraphAfter();
object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
para.Range.InsertBreak(ref oPageBreak);
string[] coulmnname = value.Split(',');
int coulmcount = dt.Rows.Count;
progressBar1.Maximum = coulmcount-1;
#region
Table firstTable = document.Tables.Add(para1.Range, coulmcount - 1, 7, ref missing, ref missing);
firstTable.Borders.Enable = 1;
int i = 0, r = 0, s = 0; string img = "";
string samplePath = "", imagepath = "";
foreach (Row row in firstTable.Rows)
{
r++;
int j = 0;
foreach (Cell cell in row.Cells)
{
progressBar1.Value = r;
//Header row
if (cell.RowIndex == 1)
{
cell.Range.Text = coulmnname[i].ToString();
cell.Range.Font.Bold = 1;
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
i++;
}
//Data row
else
{
if (j == 3)
{
img = dt.Rows[r - 2][coulmnname[j]].ToString();
if (img == "No image")
{
cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
cell.Range.Font.Name = "Century";
cell.Range.Font.Size = 14;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
j++;
}
else
{
samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
Range rngPic2 = document.Tables[1].Cell(r, 4).Range;
rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
j++;
}
}
else if (j == 4)
{
img = dt.Rows[r - 2][coulmnname[j]].ToString();
if (img == "No image")
{
cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
cell.Range.Font.Name = "Century";
cell.Range.Font.Size = 14;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
j++;
}
else
{
samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
Range rngPic2 = document.Tables[1].Cell(r, 5).Range;
rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
j++;
}
}
else if (j == 5)
{
img = dt.Rows[r - 2][coulmnname[j]].ToString();
if (img == "No image")
{
cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
cell.Range.Font.Name = "Century";
cell.Range.Font.Size = 14;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
j++;
}
else
{
samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
Range rngPic2 = document.Tables[1].Cell(r, 6).Range;
rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
j++;
}
}
else if (j == 6)
{
img = dt.Rows[r - 2][coulmnname[j]].ToString();
if (img == "No image")
{
cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
cell.Range.Font.Name = "Century";
cell.Range.Font.Size = 14;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
j++;
}
else
{
samplePath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "PostAd" + Path.DirectorySeparatorChar;
imagepath = samplePath + dt.Rows[r - 2][coulmnname[j]];
Range rngPic2 = document.Tables[1].Cell(r, 7).Range;
rngPic2.InlineShapes.AddPicture(imagepath, ref missing, ref missing, ref missing);
j++;
}
}
else
{
cell.Range.Text = dt.Rows[r - 2][coulmnname[j]].ToString();
cell.Range.Font.Name = "Century";
cell.Range.Font.Size = 14;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
j++;
}
}
}
s++;
}
#endregion
//Save the document
Random random = new Random();
int randomNumber = random.Next(0, 100);
object filename = @"E:\report" + randomNumber + ".docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
MessageBox.Show("Document created successfully in E drive in (report" + randomNumber + ".docx)");
progressBar1.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("Please Delete Existing Report's or move to another drive");
}
这篇关于在窗口应用程序中将数据表导出到ms字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文