Hi,
I am using open xml to generate the invoice report in excel sheet, so i loaded invoice no(F2), ship to(A13 to A18) and Sold to(D13 to D18) values, but when i open the excel sheet it throw the error message(Ref: Excel Problem1.png), and it's
not loading the values in ship to and sold to address values. I could not able to guess what could be an issue, please help me to solve this issue. Please referee the attachment(Excel Problem.png), this is the template i am using in my sample application
Sample Code
Program.cs
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Report is creating");
InvoiceReport productReport = new InvoiceReport("Commercial_Invoice_1");
productReport.CreateReport();
Console.WriteLine("Report is created");
Console.WriteLine("Press Enter to close");
Console.ReadLine();
}
}
InvoiceReport.cs
public class InvoiceReport
{
string path = @"E:\SampleApplication\Excel\";
string templateName = "Invoice.xlsx";
WorkbookPart wbPart = null;
SpreadsheetDocument document = null;
HelperClass helperClass = null;
InvoiceHeader invoiceHeader = null;
private InvoiceInformation invoiceInformation = null;
public InvoiceReport(string client)
{
helperClass = new HelperClass();
invoiceHeader = new InvoiceHeader();
invoiceInformation = new InvoiceInformation();
LoadInvoiceHeader();
string newFileName = path + client + ".xlsx";
helperClass.CopyFile(path + templateName, newFileName);
document = SpreadsheetDocument.Open(newFileName, true);
wbPart = document.WorkbookPart;
}
public void CreateReport()
{
string wsName = "Commercial Invoice";
//Commercial Invoice
helperClass.UpdateValue(wsName, "F2", invoiceInformation.InvoiceHeaderInfo.InvoiceNo, 0, true, wbPart);
//helperClass.UpdateValue(wsName, "F7", "Date:"+invoiceInformation.InvoiceHeaderInfo.InvoiceDate.ToShortDateString(), 0, true, wbPart);
helperClass.UpdateValue(wsName, "A13", invoiceInformation.InvoiceHeaderInfo.SoldToAddress1, 0, true, wbPart);
helperClass.UpdateValue(wsName, "A14", invoiceInformation.InvoiceHeaderInfo.SoldToAddress2, 0, true, wbPart);
helperClass.UpdateValue(wsName, "A15", invoiceInformation.InvoiceHeaderInfo.SoldToAddress3, 0, true, wbPart);
helperClass.UpdateValue(wsName, "A16", invoiceInformation.InvoiceHeaderInfo.SoldToAddress4, 0, true, wbPart);
helperClass.UpdateValue(wsName, "A17", invoiceInformation.InvoiceHeaderInfo.SoldToAddress5, 0, true, wbPart);
helperClass.UpdateValue(wsName, "D13", invoiceInformation.InvoiceHeaderInfo.ShipToAddress1, 0, true, wbPart);
helperClass.UpdateValue(wsName, "D14", invoiceInformation.InvoiceHeaderInfo.ShipToAddress2, 0, true, wbPart);
helperClass.UpdateValue(wsName, "D15", invoiceInformation.InvoiceHeaderInfo.ShipToAddress3, 0, true, wbPart);
helperClass.UpdateValue(wsName, "D16", invoiceInformation.InvoiceHeaderInfo.ShipToAddress4, 0, true, wbPart);
helperClass.UpdateValue(wsName, "D17", invoiceInformation.InvoiceHeaderInfo.ShipToAddress5, 0, true, wbPart);
document.Close();
}
private void LoadInvoiceHeader()
{
invoiceHeader = new InvoiceHeader();
List<InvoiceDetail1> invoiceDetails1 = new List<InvoiceDetail1>();
List<InvoiceDetail2> invoiceDetails2 = new List<InvoiceDetail2>();
List<InvoiceDetail> invoiceDetails = new List<InvoiceDetail>();
invoiceHeader.InvoiceNo = "480-200652";
invoiceHeader.InvoiceDate = DateTime.Now;
invoiceHeader.SoldToAddress1 = "Bahia";// Motors S.A.";
invoiceHeader.SoldToAddress2 = "Apartado Postal 6-2286";
invoiceHeader.SoldToAddress3 = "El Dorado";
invoiceHeader.SoldToAddress4 = "Panama";
invoiceHeader.SoldToAddress5 = "Rep. de Panama";
invoiceHeader.ShipToAddress1 = "Bahia Motors S.A.";
invoiceHeader.ShipToAddress2 = "Apartado Postal 6-2286";
invoiceHeader.ShipToAddress3 = "El Dorado";
invoiceHeader.ShipToAddress4 = "Panama";
invoiceHeader.ShipToAddress5 = "Rep. de Panama";
invoiceHeader.OriginPoint = "Alliston, OT";
invoiceHeader.ShippedOn = DateTime.Now;
invoiceHeader.PortOfLoading = "Jacksonville, FL";
invoiceHeader.PlaceOfDelivery = "Manzanillo, Panama";
invoiceHeader.ETA = DateTime.Now;
invoiceHeader.ShippedVIA = "Ocean";
invoiceHeader.Vessel = "BRUSSEL, V. 20";
invoiceHeader.BillOfLading = "480-183-01";
invoiceHeader.TermsOfPayment = "Net 60 Days";
invoiceInformation.InvoiceHeaderInfo = invoiceHeader;
invoiceDetails.Add(new InvoiceDetail()
{
InvoiceNo = "20H-2224-02",
Quantity = 1,
Description = "FB253EE KR CIVIC 4DR 1.8S 5MT",
UnitPrice = 500,
Amount = 500
});
invoiceDetails.Add(new InvoiceDetail()
{
InvoiceNo = "20H-2224-03",
Quantity = 2,
Description = "CR-V 5DR LX-C2WD 5AT",
UnitPrice = 200,
Amount = 400
});
invoiceDetails.Add(new InvoiceDetail()
{
InvoiceNo = "20H-2224-02",
Quantity = 4,
Description = "TAFFETA WHITE NH-578X",
UnitPrice = 500,
Amount = 2000
});
invoiceInformation.InvoiceDetails = invoiceDetails;
}
}
HelperClass.cs
public class HelperClass
{
public bool UpdateValue(string sheetName, string addressName, string value, UInt32Value styleIndex, bool isString, WorkbookPart wbPart)
{
// Assume failure.
bool updated = false;
Sheet sheet = wbPart.Workbook.Descendants<Sheet>().Where((s) => s.Name == sheetName).FirstOrDefault();
if (sheet != null)
{
Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(sheet.Id))).Worksheet;
Cell cell = InsertCellInWorksheet(ws, addressName);
if (isString)
{
// Either retrieve the index of an existing string,
// or insert the string into the shared string table
// and get the index of the new item.
int stringIndex = InsertSharedStringItem(wbPart, value);
cell.CellValue = new CellValue(stringIndex.ToString());
//cell.CellValue = new CellValue(value);
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
/*cell.DataType = CellValues.InlineString;
cell.InlineString = new InlineString() { Text = new Text(value) };*/
}
else
{
cell.CellValue = new CellValue(value);
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
}
if (styleIndex > 0)
cell.StyleIndex = styleIndex;
// Save the worksheet.
ws.Save();
updated = true;
}
return updated;
}
private Cell InsertCellInWorksheet(Worksheet ws, string addressName)
{
SheetData sheetData = ws.GetFirstChild<SheetData>();
Cell cell = null;
UInt32 rowNumber = GetRowIndex(addressName);
Row row = GetRow(sheetData, rowNumber);
// If the cell you need already exists, return it.
// If there is not a cell with the specified column name, insert one.
Cell refCell = row.Elements<Cell>().
Where(c => c.CellReference.Value == addressName).FirstOrDefault();
if (refCell != null)
{
cell = refCell;
}
else
{
cell = CreateCell(row, addressName);
}
return cell;
}
}
![ExcelProblem.png]()
Error Message
![Error Message]()