Word文档导出
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Word;
using System.Data;
using Newtonsoft.Json;
class Program
{
static string LinkSting = "Data Source=LAPTOP-MRDELPNB;Initial Catalog=ExamSystem;User ID=sa;password=12345678";
static void Main(string[] args)
{
object path;
string strContent;
MSWord.Application wordApp = new MSWord.Application();
MSWord.Document wordDoc;
path = Environment.CurrentDirectory + "//Word_Print.doc";
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;
wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;
wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdNormalView;
wordApp.ActiveWindow.View.Type =WdViewType.wdOutlineView;
wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdNormalView;
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
#region 页码设置并添加页码
MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;
pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
pns.HeadingLevelForChapter = 0;
pns.IncludeChapterNumber = false;
pns.RestartNumberingAtSection = false;
pns.StartingNumber = 0;
object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;
object first = true;
wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
#endregion
object unite = MSWord.WdUnits.wdStory;
try
{
string Sql = "select TopicName,TopicId from Topic;select TopicId,OptionsName,OptionsYes from Options";
using (SqlConnection conn=new SqlConnection(LinkSting))
{
using (SqlDataAdapter Sda=new SqlDataAdapter(Sql, conn))
{
DataSet Ds = new DataSet();
Sda.Fill(Ds);
List<TopicModel> topicModels = JsonConvert.DeserializeObject<List<TopicModel>>(JsonConvert.SerializeObject(Ds.Tables[0]));
List<OptionsModel> OptionsModel = JsonConvert.DeserializeObject<List<OptionsModel>>(JsonConvert.SerializeObject(Ds.Tables[1]));
strContent = "考试\n";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
string Da = "";
for (int i = 0; i < topicModels.Count; i++)
{
wordDoc.Paragraphs.Last.Range.Text += $"{i+1}.{topicModels[i].TopicName}()";
List< OptionsModel > optionsModels= OptionsModel.Where(s => s.TopicId == topicModels[i].TopicId).ToList();
for (int j = 0; j < optionsModels.Count; j++)
{
wordDoc.Paragraphs.Last.Range.Text += $"{Convert.ToChar(j+65)}.{optionsModels[j].OptionsName}";
Da += optionsModels[j].OptionsYes;
if (j== optionsModels.Count-1)
{
wordDoc.Paragraphs.Last.Range.Text += $"答案:{Da}";
Da = "";
}
}
}
}
}
}
catch (Exception e)
{
throw;
}
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Content.InsertAfter(DateTime.Now.ToLongDateString());
object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 创建完毕!");
Console.ReadKey();
}