123 lines
4.8 KiB
C#
123 lines
4.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using Xceed.Words.NET;
|
|
using Xceed.Document.NET;
|
|
|
|
namespace QuizMaster
|
|
{
|
|
public partial class Form2 : Form
|
|
{
|
|
private string _content;
|
|
public string MaDe { get; set; }
|
|
|
|
public Form2(string content)
|
|
{
|
|
InitializeComponent();
|
|
_content = content;
|
|
txtResult.Text = _content.Replace("\n", Environment.NewLine);
|
|
}
|
|
|
|
private void btnExportWord_Click(object sender, EventArgs e)
|
|
{
|
|
using (SaveFileDialog saveDialog = new SaveFileDialog())
|
|
{
|
|
saveDialog.Filter = "Word Document|*.docx";
|
|
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
|
|
|
string defaultFileName = this.Text.Contains("CÓ ĐÁP ÁN") ? $"DeTracNhiem_{MaDe}_DapAn_{timestamp}.docx": $"DeTracNhiem_{MaDe}_{timestamp}.docx";
|
|
saveDialog.FileName = defaultFileName;
|
|
|
|
if (saveDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
using (var doc = DocX.Create(saveDialog.FileName))
|
|
{
|
|
doc.InsertParagraph(this.Text)
|
|
.FontSize(16).Bold().Alignment = Alignment.center;
|
|
|
|
ExportContentWithFormatting(doc, _content);
|
|
|
|
doc.Save();
|
|
}
|
|
|
|
MessageBox.Show("Xuất file Word thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Lỗi khi xuất Word: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ExportContentWithFormatting(DocX doc, string content)
|
|
{
|
|
string[] lines = content.Split('\n');
|
|
|
|
foreach (string line in lines)
|
|
{
|
|
string trimmedLine = line.Trim();
|
|
|
|
if (string.IsNullOrEmpty(trimmedLine))
|
|
{
|
|
doc.InsertParagraph();
|
|
continue;
|
|
}
|
|
|
|
Paragraph paragraph;
|
|
|
|
if (trimmedLine.StartsWith("Câu ") && trimmedLine.Contains(":"))
|
|
{
|
|
paragraph = doc.InsertParagraph(trimmedLine);
|
|
paragraph.FontSize(12).Bold();
|
|
paragraph.SpacingAfter(12);
|
|
}
|
|
else if (trimmedLine.StartsWith("Mã đề:"))
|
|
{
|
|
if (this.Text.Contains("CÓ ĐÁP ÁN"))
|
|
{
|
|
var codeParagraph = doc.InsertParagraph($"Mã đề: {MaDe}");
|
|
codeParagraph.FontSize(14).Bold().Alignment = Alignment.center;
|
|
doc.InsertParagraph(new string('_', 75)).Bold().SpacingBefore(6).SpacingAfter(6).Alignment = Alignment.center;
|
|
}
|
|
else
|
|
{
|
|
var codeParagraph = doc.InsertParagraph($"Mã đề: {MaDe}");
|
|
codeParagraph.FontSize(14).Bold().Alignment = Alignment.center;
|
|
|
|
string hoTen = "Họ và tên: " + new string('.', 106);
|
|
string sdt = "Số điện thoại: " + new string('.', 100);
|
|
string email = "Email: " + new string('.', 113);
|
|
|
|
doc.InsertParagraph(hoTen).FontSize(12).SpacingBefore(10);
|
|
doc.InsertParagraph(sdt).FontSize(12).SpacingBefore(6);
|
|
doc.InsertParagraph(email).FontSize(12).SpacingBefore(6).SpacingAfter(18);
|
|
doc.InsertParagraph(new string('_', 75)).Bold().SpacingAfter(6).Alignment = Alignment.center;
|
|
}
|
|
|
|
}
|
|
else if (trimmedLine.Length > 2 && trimmedLine[1] == '.')
|
|
{
|
|
paragraph = doc.InsertParagraph();
|
|
string label = trimmedLine.Substring(0, 2);
|
|
string text = trimmedLine.Substring(2).Trim();
|
|
|
|
paragraph.Append(label).Bold();
|
|
paragraph.Append(" " + text);
|
|
paragraph.FontSize(11);
|
|
paragraph.SpacingAfter(6);
|
|
}
|
|
else
|
|
{
|
|
paragraph = doc.InsertParagraph(trimmedLine);
|
|
paragraph.FontSize(11);
|
|
}
|
|
}
|
|
string end = new string('_', 20) + "Hết" + new string('_', 20);
|
|
doc.InsertParagraph(end).Bold().FontSize(12).SpacingAfter(6).SpacingBefore(18).Alignment = Alignment.center;
|
|
}
|
|
}
|
|
}
|