I'm trying to create a invoice generation windows form application using C# and spire.XLS. I've attached the desired output result invoice (named
LOL ).Desired_Output.xlsx
Basically I'm trying to get the items(i.e. Products names) that I select in the combo box and when I hit a button then that same item i.e. comboBox1.text gets copied to a specific cell/cells along with other info (Batch No, MFG & EXP date) in the next row/rows (image attached). Then when I select another item from the combo box then the above step happens but the data gets written in a new row and not overwrite the earlier entered data. (Hope when you see
you will get what I'm trying to say, english isn't my first language )Desired_Output.xlsx
I've done the below code which generated the excel template for the invoice:
- Code: Select all
private void InvoiceTemplate()
{
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "Test";
sheet.PageSetup.TopMargin = 0.5;
sheet.PageSetup.BottomMargin = 0.5;
sheet.PageSetup.LeftMargin = 0.3;
sheet.PageSetup.RightMargin = 0.3;
sheet.PageSetup.HeaderMarginInch = 0.3;
sheet.PageSetup.FooterMarginInch = 0.3;
sheet.Range["J52:J53"].Borders[BordersLineType.EdgeLeft].LineStyle=LineStyleType.Thin;
sheet.Range["J53:J53"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Double;
sheet.Range["A2:A60"].Borders[BordersLineType.EdgeLeft].LineStyle=LineStyleType.Thin;
sheet.Range["J2:J60"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["G2:G3"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["G12:G13"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["G22:G51"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["H22:H51"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["I22:I51"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["D2:D21"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["B15:B21"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["E3:J3"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["E5:J5"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["E7:J7"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["E11:J11"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["E13:J13"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A21:J21"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A7:D7"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A14:D14"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A16:D16"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A18:D18"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["D2:D21"].Borders[BordersLineType.EdgeRight].LineStyle=LineStyleType.Thin;
sheet.Range["A52:J52"].Borders[BordersLineType.EdgeTop].LineStyle=LineStyleType.Thin;
sheet.Range["A60:J60"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["G55:J55"].Borders[BordersLineType.EdgeTop].LineStyle=LineStyleType.Thin;
sheet.Range["G55:G60"].Borders[BordersLineType.EdgeLeft].LineStyle=LineStyleType.Thin;
sheet.Range["J52:J53"].Merge();
sheet.Range["A1:J1"].Merge();
sheet.SetRowHeight(1,17.25);
sheet.Range["A1"].Text = "INVOICE";
sheet.Range["A1"].Style.Font.FontName="Cambria";
sheet.Range["A1"].Style.Font.Size=11;
sheet.Range["A1"].Style.Font.IsBold=true;
sheet.Range["A1"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A1"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["A1:J1"].Borders[BordersLineType.EdgeBottom].LineStyle=LineStyleType.Thin;
sheet.Range["A3:D3"].Merge();
sheet.Range["A3"].Text = "RALSONS DORMA PRIVATE LIMITED";
sheet.Range["A3"].Style.Font.FontName="Cambria";
sheet.Range["A3"].Style.Font.Size=10;
sheet.Range["A3"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A3"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A4:D7"].Merge();
sheet.Range["A4"].Text = "31/S/7, YO-HIC-SINH PARANI,\r\nSILPARA - 700 031, INDIA\r\nSTATE CODE:-19; GSTIN NO. 99PAECL1620T1OD\r\nTEL : 00 91 44 2585 4270, FAX : 00 91 44 2585 4520";
sheet.Range["A4"].Style.Font.FontName="Cambria";
sheet.Range["A4"].Style.Font.Size=9;
sheet.Range["A4"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A4"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A2:D2"].Merge();
sheet.Range["A2"].Text = "Exporter";
sheet.Range["A2"].Style.Font.FontName="Cambria";
sheet.Range["A2"].Style.Font.Size=10;
sheet.Range["A2"].Style.Font.IsBold=true;
sheet.Range["A2"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A2"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E2:G2"].Merge();
sheet.Range["E2"].Text = "Invoice No. & Date";
sheet.Range["E2"].Style.Font.FontName="Cambria";
sheet.Range["E2"].Style.Font.Size=10;
sheet.Range["E2"].Style.Font.IsBold=true;
sheet.Range["E2"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E2"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["H2:J2"].Merge();
sheet.Range["H2"].Text = "Exporter's Ref.";
sheet.Range["H2"].Style.Font.FontName="Cambria";
sheet.Range["H2"].Style.Font.Size=10;
sheet.Range["H2"].Style.Font.IsBold=true;
sheet.Range["H2"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["H2"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E4:J4"].Merge();
sheet.Range["E4"].Text = "Buyer's Order No & Date";
sheet.Range["E4"].Style.Font.FontName="Cambria";
sheet.Range["E4"].Style.Font.Size=10;
sheet.Range["E4"].Style.Font.IsBold=true;
sheet.Range["E4"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E4"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E6:J6"].Merge();
sheet.Range["E6"].Text = "Other Reference(s)";
sheet.Range["E6"].Style.Font.FontName="Cambria";
sheet.Range["E6"].Style.Font.Size=10;
sheet.Range["E6"].Style.Font.IsBold=true;
sheet.Range["E6"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E6"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A8:D8"].Merge();
sheet.Range["A8"].Text = "Consignee";
sheet.Range["A8"].Style.Font.FontName="Cambria";
sheet.Range["A8"].Style.Font.Size=10;
sheet.Range["A8"].Style.Font.IsBold=true;
sheet.Range["A8"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A8"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E8:J8"].Merge();
sheet.Range["E8"].Text = "Buyer (if other than consignee)";
sheet.Range["E8"].Style.Font.FontName="Cambria";
sheet.Range["E8"].Style.Font.Size=10;
sheet.Range["E8"].Style.Font.IsBold=true;
sheet.Range["E8"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E8"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E12:G12"].Merge();
sheet.Range["E12"].Text = "Country of Origin of Goods";
sheet.Range["E12"].Style.Font.FontName="Cambria";
sheet.Range["E12"].Style.Font.Size=10;
sheet.Range["E12"].Style.Font.IsBold=true;
sheet.Range["E12"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E12"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["H12:J12"].Merge();
sheet.Range["H12"].Text = "Country of Final Destination";
sheet.Range["H12"].Style.Font.FontName="Cambria";
sheet.Range["H12"].Style.Font.Size=10;
sheet.Range["H12"].Style.Font.IsBold=true;
sheet.Range["H12"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["H12"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["E14:J14"].Merge();
sheet.Range["E14"].Text = "Terms of Delivery and Payment";
sheet.Range["E14"].Style.Font.FontName="Cambria";
sheet.Range["E14"].Style.Font.Size=10;
sheet.Range["E14"].Style.Font.IsBold=true;
sheet.Range["E14"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E14"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A15:B15"].Merge();
sheet.Range["A15"].Text = "Pre Carriage by";
sheet.Range["A15"].Style.Font.FontName="Cambria";
sheet.Range["A15"].Style.Font.Size=10;
sheet.Range["A15"].Style.Font.IsBold=true;
sheet.Range["A15"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A15"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["C15:D16"].Merge();
sheet.Range["C15"].Text = "Place of Receipt by Pre-Carrier";
sheet.Range["C15"].Style.Font.FontName="Cambria";
sheet.Range["C15"].Style.Font.Size=8;
sheet.Range["C15"].Style.Font.IsBold=true;
sheet.Range["C15"].Style.VerticalAlignment=VerticalAlignType.Top;
sheet.Range["C15"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A17:B17"].Merge();
sheet.Range["A17"].Text = "Vessel / Flight No.";
sheet.Range["A17"].Style.Font.FontName="Cambria";
sheet.Range["A17"].Style.Font.Size=10;
sheet.Range["A17"].Style.Font.IsBold=true;
sheet.Range["A17"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A17"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["C17:D17"].Merge();
sheet.Range["C17"].Text = "Port of Loading";
sheet.Range["C17"].Style.Font.FontName="Cambria";
sheet.Range["C17"].Style.Font.Size=10;
sheet.Range["C17"].Style.Font.IsBold=true;
sheet.Range["C17"].Style.VerticalAlignment=VerticalAlignType.Top;
sheet.Range["C17"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E18"].Text = "PAYMENT :";
sheet.Range["E18"].Style.Font.FontName="Cambria";
sheet.Range["E18"].Style.Font.Size=10;
sheet.Range["E18"].Style.Font.IsBold=true;
sheet.Range["E18"].Style.VerticalAlignment=VerticalAlignType.Top;
sheet.Range["E18"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A19:B19"].Merge();
sheet.Range["A19"].Text = "Port of Discharge";
sheet.Range["A19"].Style.Font.FontName="Cambria";
sheet.Range["A19"].Style.Font.Size=10;
sheet.Range["A19"].Style.Font.IsBold=true;
sheet.Range["A19"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A19"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["C19:D19"].Merge();
sheet.Range["C19"].Text = "Final Destination";
sheet.Range["C19"].Style.Font.FontName="Cambria";
sheet.Range["C19"].Style.Font.Size=10;
sheet.Range["C19"].Style.Font.IsBold=true;
sheet.Range["C19"].Style.VerticalAlignment=VerticalAlignType.Top;
sheet.Range["C19"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A20:B21"].Merge();
sheet.Range["C20:D21"].Merge();
sheet.Range["A22:B23"].Merge();
sheet.Range["A22"].Text = "Marks & Nos./\r\nContainer No.";
sheet.Range["A22"].Style.Font.FontName="Cambria";
sheet.Range["A22"].Style.Font.Size=11;
sheet.Range["A22"].Style.Font.IsBold=true;
sheet.Range["A22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A22"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["C22:D23"].Merge();
sheet.Range["C22"].Text = "No. & Kind of Pkgs.";
sheet.Range["C22"].Style.Font.FontName="Cambria";
sheet.Range["C22"].Style.Font.Size=11;
sheet.Range["C22"].Style.Font.IsBold=true;
sheet.Range["C22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["C22"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["E22:G23"].Merge();
sheet.Range["E22"].Text = "Description of Goods";
sheet.Range["E22"].Style.Font.FontName="Cambria";
sheet.Range["E22"].Style.Font.Size=11;
sheet.Range["E22"].Style.Font.IsBold=true;
sheet.Range["E22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E22"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["E24:G24"].Merge();
sheet.Range["E24"].Text = "SKIN CARE PRODUCTS";
sheet.Range["E24"].Style.Font.FontName="Cambria";
sheet.Range["E24"].Style.Font.Size=12;
sheet.Range["E24"].Style.Font.IsBold=true;
sheet.Range["E24"].Style.Font.Underline=FontUnderlineType.Single;
sheet.Range["E24"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["E24"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["H22:H23"].Merge();
sheet.Range["H22"].Text = "Quantity";
sheet.Range["H22"].Style.Font.FontName="Cambria";
sheet.Range["H22"].Style.Font.Size=11;
sheet.Range["H22"].Style.Font.IsBold=true;
sheet.Range["H22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["H22"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["I22"].Text = "Rate";
sheet.Range["I22"].Style.Font.FontName="Cambria";
sheet.Range["I22"].Style.Font.Size=10;
sheet.Range["I22"].Style.Font.IsBold=true;
sheet.Range["I22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["I22"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["J22"].Text = "Amount";
sheet.Range["J22"].Style.Font.FontName="Cambria";
sheet.Range["J22"].Style.Font.Size=10;
sheet.Range["J22"].Style.Font.IsBold=true;
sheet.Range["J22"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["J22"].Style.HorizontalAlignment=HorizontalAlignType.Center;
sheet.Range["I23:J23"].Merge();
sheet.Range["I23"].Text="U.S.DOLLARS";
sheet.Range["I23"].Style.Font.FontName="Cambria";
sheet.Range["I23"].Style.Font.Size=10;
sheet.Range["I23"].Style.Font.IsBold=true;
sheet.Range["I23"].Style.Font.Underline=FontUnderlineType.Single;
sheet.Range["I23"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["I23"].Style.HorizontalAlignment=HorizontalAlignType.Center;
//LAST LINES
sheet.Range["A52"].Text="Amount Chargeable";
sheet.Range["A52"].Style.Font.FontName="Cambria";
sheet.Range["A52"].Style.Font.Size=10;
sheet.Range["A52"].Style.Font.IsBold=true;
sheet.Range["A52"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A52"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A53"].Text="(in words)";
sheet.Range["A53"].Style.Font.FontName="Cambria";
sheet.Range["A53"].Style.Font.Size=10;
sheet.Range["A53"].Style.Font.IsBold=true;
sheet.Range["A53"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A53"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A54"].Text="I.E. CODE NO.0200008602";
sheet.Range["A54"].Style.Font.FontName="Cambria";
sheet.Range["A54"].Style.Font.Size=10;
sheet.Range["A54"].Style.Font.IsBold=true;
sheet.Range["A54"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A54"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["G55"].Text="Signature & Date";
sheet.Range["G55"].Style.Font.FontName="Cambria";
sheet.Range["G55"].Style.Font.Size=10;
sheet.Range["G55"].Style.Font.IsBold=true;
sheet.Range["G55"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["G55"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A57"].Text="Declaration :";
sheet.Range["A57"].Style.Font.FontName="Cambria";
sheet.Range["A57"].Style.Font.Size=10;
sheet.Range["A57"].Style.Font.IsBold=true;
sheet.Range["A57"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A57"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A58"].Text="We declare that this Invoice shows the actual price of the";
sheet.Range["A58"].Style.Font.FontName="Cambria";
sheet.Range["A58"].Style.Font.Size=10;
sheet.Range["A58"].Style.Font.IsBold=true;
sheet.Range["A58"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A58"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.Range["A59"].Text="goods described that all particulars are true and correct";
sheet.Range["A59"].Style.Font.FontName="Cambria";
sheet.Range["A59"].Style.Font.Size=10;
sheet.Range["A59"].Style.Font.IsBold=true;
sheet.Range["A59"].Style.VerticalAlignment=VerticalAlignType.Center;
sheet.Range["A59"].Style.HorizontalAlignment=HorizontalAlignType.Left;
sheet.SetColumnWidth(1,11.57);
sheet.SetColumnWidth(2,7.57);
sheet.SetColumnWidth(3,9.71);
sheet.SetColumnWidth(4,12.43);
sheet.SetColumnWidth(5,10.57);
sheet.SetColumnWidth(6,7.29);
sheet.SetColumnWidth(7,9.43);
sheet.SetColumnWidth(8,7.57);
sheet.SetColumnWidth(9,7);
sheet.SetColumnWidth(10,11.72);
workbook.SaveToFile("sample.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("sample.xlsx");
}
Also, the combo box has the following code at the form activated event:
- Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\Jay\Proj\products.xlsx");
Worksheet sheet = workbook.Worksheets[0];
CellRange[] items = sheet.Columns[0].Cells;
foreach (CellRange item in items)
{
if (!String.IsNullOrEmpty(item.Value2.ToString()))
{
comboBox1.Items.Add(item.Value);
}
}
But I'm struggling to get the combo box items to the excel file in such a way so that I can write the below data side by side :
"ABC PHARMA INC
MANILA"
1/247 TO 247/247 =
247 CARTONS
1/46 TO 46/ 46 =
46 CARTONS
TOTAL 293 CARTONS
and
293 NOS
EXPORT STANDARD CARTONS
in such a way shown in the desired output file.
Also, if the product name data are too many then the program needs to automatically create more pages of the invoice with the same header and footer rows (i.e. Range["A1:J21"] and Range["A52:J60"] should remain same).
Apologies again for the bad english.
Please help