- Code: Select all
Document headerDoc = new Document ( Application.StartupPath + "\\headerWH.docx" ) ;
Document doc = new Document () ;
Section s = doc.AddSection () ;
foreach (Section sec in headerDoc.Sections)
{
for (int i = 0; i < sec.Body.ChildObjects.Count; i++)
{
doc.Sections[0].Body.ChildObjects.Add(sec.Body.ChildObjects[i].Clone());
}
}
Table table = s.AddTable(true) ;
doc.Sections[0].PageSetup.Margins.Left = 35.9F;
doc.Sections[0].PageSetup.Margins.Right = 35.9F;
doc.Sections[0].PageSetup.Orientation = PageOrientation.Landscape;
String[] Header = {"ሀ)ተ.ቁ (No)","ለ)የግብር ከፋዩ መለያ ቁጥር (TIN)","ሐ)ግብር ተቀናሽ የተደረገበት ሰው/ ድርጅት ሙሉ ስም (Supplier)",
"መ)አድራሻ","ሠ)የገቢ ዓይነት /ከህንጻ ኪራይ / ሌሎች/ (Income Type)","ረ)የተከፋይ መጠን (Amount)","ሰ)ተቀናሽ የተደረገ ግብር (Withheld Amount) (2 %)",
"ሸ)ከተከፋይ ሂሳብ ላይ ለተቀነሰ ግብር የተሰጠ ደረሰኝ" };
String[] Header2 = { "ክልል (Region)", "ዞን / ክፍለ ከተማ (Subcity)", "ወረዳ (Woreda)",
"ቀበሌ / ገበሬ ማሀበር (Kebele)", "የቤት ቁጥር (H.No)", "የደረሰኝ ቁጥር (Reciept No)", "ቀን Date"};
int rs = dataGridView1.Rows.Count;
int cs = dataGridView1.Columns.Count;
String[][] data = new String[rs][];
double totalamount = 0;
double totalwithholding = 0;
double firstpageamount = 0;
double firstpagewh = 0;
double totalotherpagewh = 0;
double totalotehrpageamount = 0;
int max = 7;
if (data.Length < 7)
max = data.Length;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[9].Value != null)
{
totalamount += Double.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString());
if (i >= max)
totalotehrpageamount += Double.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString());
if (i < max)
firstpageamount += Double.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString());
}
if (dataGridView1.Rows[i].Cells[10].Value != null)
{
totalwithholding += Double.Parse(dataGridView1.Rows[i].Cells[10].Value.ToString());
if (i >= max)
totalotherpagewh += Double.Parse(dataGridView1.Rows[i].Cells[10].Value.ToString());
if (i < max)
firstpagewh += Double.Parse(dataGridView1.Rows[i].Cells[9].Value.ToString());
}
data[i] = new String[13];
for (int j = 0 ; j < 13 ; j++)
{
if (j == 12)
data[i][j] = dataGridView1.Rows[i].Cells[13].Value.ToString() + " " + dataGridView1.Rows[i].Cells[12].Value.ToString() + ", " + dataGridView1.Rows[i].Cells[14].Value.ToString();
else if (j == 1)
data[i][2] = dataGridView1.Rows[i].Cells[1].Value.ToString();
else if (j == 2)
data[i][1] = dataGridView1.Rows[i].Cells[2].Value.ToString();
else if ( j == 9 || j == 10 )
{
try
{
data[i][j] = String.Format("{0:N}", Double.Parse(dataGridView1.Rows[i].Cells[j].Value.ToString()));
}
catch
{
data[i][j] = "0.00";
}
}
else
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
data[i][j] = dataGridView1.Rows[i].Cells[j].Value.ToString();
else
data[i][j] = "";
}
}
}
table.ResetCells(max+4, 13);
TableRow FRow = table.Rows[0];
TableRow FRow2 = table.Rows[1];
table.ApplyVerticalMerge(0, 0, 1);
table.ApplyVerticalMerge(1, 0, 1);
table.ApplyVerticalMerge(2, 0, 1);
table.ApplyVerticalMerge(8, 0, 1);
table.ApplyVerticalMerge(9, 0, 1);
table.ApplyVerticalMerge(10, 0, 1);
table.ApplyHorizontalMerge(0, 3, 7);
table.ApplyHorizontalMerge(0, 11, 12);
table.ApplyHorizontalMerge(max + 2, 0, 8);
table.ApplyHorizontalMerge(max + 2, 11, 12);
table.ApplyHorizontalMerge(max + 3, 0, 8);
table.ApplyHorizontalMerge(max + 3, 11, 12);
FRow.IsHeader = true;
FRow.Height = 18;
FRow2.IsHeader = true;
FRow2.Height = 18;
for (int i = 0; i < 8; i++)
{
Paragraph p;
if (i == 0)
FRow.Cells[i].Width = 10;
if (i == 1 || i == 2)
FRow.Cells[i].Width = 75;
if (i == 4 || i == 5 || i == 6 || i == 7)
{
FRow.Cells[i + 4].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p = FRow.Cells[i + 4].AddParagraph();
}
else
p = FRow.Cells[i].AddParagraph();
FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange TR = p.AppendText(Header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 8;
TR.CharacterFormat.TextColor = Color.Black;
TR.CharacterFormat.Bold = true;
}
for (int i = 0; i < 7; i++)
{
Paragraph p = null ;
if (i >= 0 && i <= 4)
p = FRow2.Cells[i + 3].AddParagraph() ;
else
p = FRow2.Cells[i + 6].AddParagraph() ;
if (i + 6 == 12)
FRow2.Cells[i + 6].Width = 75 ;
FRow2.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange TR = p.AppendText(Header2[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 8;
TR.CharacterFormat.TextColor = Color.Black;
TR.CharacterFormat.Bold = true;
}
for (int r = 0; r <= max + 1; r++)
{
TableRow DataRow = table.Rows[r + 2];
DataRow.Height = 14;
if (r == max)
{
Paragraph p2 = DataRow.Cells[0].AddParagraph();
TextRange TR2 = p2.AppendText("ሌሎች አባሪ ገጾች ድምር");
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
TR2.CharacterFormat.Bold = true;
DataRow.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
Paragraph p4 = DataRow.Cells[9].AddParagraph();
TextRange TR4 = p4.AppendText(String.Format("{0:N}", totalotehrpageamount));
TR4.CharacterFormat.FontName = "Calibri";
TR4.CharacterFormat.FontSize = 8;
TR4.CharacterFormat.TextColor = Color.Black;
TR4.CharacterFormat.Bold = true;
p4.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
DataRow.Cells[9].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p6 = DataRow.Cells[10].AddParagraph();
TextRange TR6 = p6.AppendText(String.Format("{0:N}", totalotherpagewh ));
TR6.CharacterFormat.FontName = "Calibri";
TR6.CharacterFormat.FontSize = 8;
TR6.CharacterFormat.TextColor = Color.Black;
TR6.CharacterFormat.Bold = true;
p6.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
DataRow.Cells[10].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
}
else if (r == max + 1)
{
Paragraph p2 = DataRow.Cells[0].AddParagraph();
TextRange TR2 = p2.AppendText ("ጠቅላላ ድምር (ረ)እና (ሰ)(Line 20 and 30)");
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
TR2.CharacterFormat.Bold = true;
DataRow.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p4 = DataRow.Cells[9].AddParagraph();
TextRange TR4 = p4.AppendText(String.Format("{0:N}", totalamount));
TR4.CharacterFormat.FontName = "Calibri";
TR4.CharacterFormat.FontSize = 8;
TR4.CharacterFormat.TextColor = Color.Black;
TR4.CharacterFormat.Bold = true;
p4.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
DataRow.Cells[9].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p6 = DataRow.Cells[10].AddParagraph();
TextRange TR6 = p6.AppendText(String.Format("{0:N}", totalwithholding ));
TR6.CharacterFormat.FontName = "Calibri";
TR6.CharacterFormat.FontSize = 8;
TR6.CharacterFormat.TextColor = Color.Black;
TR6.CharacterFormat.Bold = true;
p6.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
DataRow.Cells[10].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
}
else
{
for (int c = 0; c < 13 ; c++)
{
if ( c == 0 )
DataRow.Cells[c].Width = 10 ;
if ( c == 1 || c == 2 || c == 8 )
{
FRow.Cells[c].Width = 75;
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
}
else
{
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r][c]);
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
}
}
}
}
Document footerDoc = new Document (Application.StartupPath + "\\footerWH.docx");
foreach (Section sec in footerDoc.Sections)
{
for (int i = 0; i < sec.Body.ChildObjects.Count; i++)
{
doc.Sections[0].Body.ChildObjects.Add(sec.Body.ChildObjects[i].Clone());
}
//Paragraph p1 = doc.Sections[0].Tables[0].Rows[0].Cells[2].AddParagraph();
//MessageBox.Show(doc.Sections[0].Tables[0].Rows[0].Cells[2].Paragraphs[0].Text);
Paragraph p1 = doc.Sections[0].Tables[3].Rows[0].Cells[2].AddParagraph();
TextRange TR1 = p1.AppendText(cs.ToString ());
TR1.CharacterFormat.FontName = "Calibri";
TR1.CharacterFormat.FontSize = 8;
TR1.CharacterFormat.TextColor = Color.Black;
TR1.CharacterFormat.Bold = true;
Paragraph p2 = doc.Sections[0].Tables[3].Rows[1].Cells[2].AddParagraph();
TextRange TR2 = p2.AppendText(String.Format("{0:N}", totalamount));
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
TR2.CharacterFormat.Bold = true;
Paragraph p3 = doc.Sections[0].Tables[3].Rows[2].Cells[2].AddParagraph();
TextRange TR3 = p3.AppendText(String.Format("{0:N}",totalwithholding));
TR3.CharacterFormat.FontName = "Calibri";
TR3.CharacterFormat.FontSize = 8;
TR3.CharacterFormat.TextColor = Color.Black;
TR3.CharacterFormat.Bold = true;
DateTime dt = DateTime.Now;
string date = "";
if (calendar == "Ethiopian")
date = EthiopianCalendar.Conversion.EthiopianDateConverter.ToEthiopianDate(dt).ToShortDateString() + " E.C.";
if (calendar == "Gregorian")
date = dt.ToShortDateString() + " G.C.";
Paragraph p4 = doc.Sections[0].Tables[4].Rows[0].Cells[0].Paragraphs[1];
TextRange TR4 = p4.AppendText(date.ToString());
TR4.CharacterFormat.FontName = "Calibri";
TR4.CharacterFormat.FontSize = 8;
TR4.CharacterFormat.TextColor = Color.Black;
TR4.CharacterFormat.Bold = true;
}
Document headerDoc2 = new Document(Application.StartupPath + "\\headerWHPage2.docx");
Section s2 = doc.AddSection();
foreach (Section sec in headerDoc2.Sections)
{
for (int i = 0; i < sec.Body.ChildObjects.Count; i++)
{
doc.Sections[1].Body.ChildObjects.Add(sec.Body.ChildObjects[i].Clone());
}
}
Table table2 = s2.AddTable(true);
doc.Sections[1].PageSetup.Margins.Top = 35.9F;
doc.Sections[1].PageSetup.Margins.Bottom = 35.9F;
doc.Sections[1].PageSetup.Margins.Left = 35.9F;
doc.Sections[1].PageSetup.Margins.Right = 35.9F;
doc.Sections[1].PageSetup.Orientation = PageOrientation.Landscape;
String[] secHeader = {"ሀ)ተ.ቁ (No)","ለ)የግብር ከፋዩ መለያ ቁጥር (TIN)","ሐ)ግብር ተቀናሽ የተደረገበት ሰው/ ድርጅት ሙሉ ስም (Supplier)",
"መ)አድራሻ","ሠ)የገቢ ዓይነት /ከህንጻ ኪራይ / ሌሎች/ (Income Type)","ረ)የተከፋይ መጠን (Amount)","ሰ)ተቀናሽ የተደረገ ግብር (Withheld Amount) (2 %)",
"ሸ)ከተከፋይ ሂሳብ ላይ ለተቀነሰ ግብር የተሰጠ ደረሰኝ" };
String[] secHeader2 = { "ክልል (Region)", "ዞን / ክፍለ ከተማ (Subcity)", "ወረዳ (Woreda)",
"ቀበሌ / ገበሬ ማሀበር (Kebele)", "የቤት ቁጥር (H.No)", "የደረሰኝ ቁጥር (Reciept No)", "ቀን (Date)"};
if (data.Length > 7)
{
table2.ResetCells(data.Length - max + 2, 13);
TableRow twoFRow = table2.Rows[0];
TableRow twoFRow2 = table2.Rows[1];
table2.ApplyVerticalMerge(0, 0, 1);
table2.ApplyVerticalMerge(1, 0, 1);
table2.ApplyVerticalMerge(2, 0, 1);
table2.ApplyVerticalMerge(8, 0, 1);
table2.ApplyVerticalMerge(9, 0, 1);
table2.ApplyVerticalMerge(10, 0, 1);
table2.ApplyHorizontalMerge(0, 3, 7);
table2.ApplyHorizontalMerge(0, 11, 12);
//table2.ApplyHorizontalMerge(table2.Rows.Count - 2, 0, 8);
//table2.ApplyHorizontalMerge(table2.Rows.Count - 2, 11, 12);
//table2.ApplyHorizontalMerge(table.Rows.Count - 1, 0, 8);
//table2.ApplyHorizontalMerge(table.Rows.Count - 1, 11, 12);
twoFRow.IsHeader = true;
twoFRow.Height = 18;
twoFRow2.IsHeader = true;
twoFRow2.Height = 18;
for (int i = 0; i < 8; i++)
{
Paragraph p;
if (i == 0)
twoFRow.Cells[i].Width = 10;
if (i == 1 || i == 2)
twoFRow.Cells[i].Width = 75;
if (i == 4 || i == 5 || i == 6 || i == 7)
{
twoFRow.Cells[i + 4].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p = twoFRow.Cells[i + 4].AddParagraph();
}
else
p = twoFRow.Cells[i].AddParagraph();
twoFRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange TR = p.AppendText(Header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 8;
TR.CharacterFormat.TextColor = Color.Black;
TR.CharacterFormat.Bold = true;
}
for (int i = 0; i < 7; i++)
{
Paragraph p = null;
if (i >= 0 && i <= 4)
p = twoFRow2.Cells[i + 3].AddParagraph();
else
p = twoFRow2.Cells[i + 6].AddParagraph();
if (i + 6 == 12)
twoFRow2.Cells[i + 6].Width = 75;
twoFRow2.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange TR = p.AppendText(Header2[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 8;
TR.CharacterFormat.TextColor = Color.Black;
TR.CharacterFormat.Bold = true;
}
for (int r = 0; r<data .Length-max ; r++)
{
TableRow DataRow = table2.Rows[r + 2];
DataRow.Height = 14;
for (int c = 0; c < data[r].Length; c++)
{
if (c == 0)
DataRow.Cells[c].Width = 10;
if (c == 1 || c == 2 || c == 8)
{
FRow.Cells[c].Width = 75;
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r+max][c]);
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
}
else
{
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(data[r+max][c]);
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 8;
TR2.CharacterFormat.TextColor = Color.Black;
}
}
}
}
try
{
string origFileName = Path.Combine (Application.StartupPath, "Declerations", c, y, m, "WH.docx");
doc.SaveToFile ( origFileName, FileFormat.Docx ) ;
string directory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string origFileName2 = Path.Combine(directory, "VirtualStore", "Program Files (x86)", "Tonetor Cyberbiz", "Tonetor Tax Decleration", "Declerations", c, y, m, "WH.docx");
if (File.Exists(origFileName2))
System.Diagnostics.Process.Start ( @origFileName2 ) ;
else
System.Diagnostics.Process.Start ( @origFileName ) ;
}
catch ( Exception ex )
{
MessageBox.Show( ex.Message + "Can't create a new document. The report may be open. Close it to create a new one.", Properties.Resources.AppName);
}