Esempio n. 1
0
        /// <summary>
        /// This generate function do same work as of reportProcessor generate function. But it is different from that generate function, as it will also used to generate multiline report, when taking values from purchase.
        /// </summary>
        /// <param name="field"></param>
        /// <param name="reportName"></param>
        /// <param name="saveFileName"></param>
        public void GenerateReport(Field field, string reportName, string saveFileName)
        {
            string     documentName = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(reportName));
            FileStream fileStream   = new FileStream(reportName, FileMode.Open, FileAccess.Read);
            IWorkbook  workbook     = null;
            string     extension    = Utility.GetExtension(reportName);

            if (extension == ".xlsx")
            {
                workbook = new XSSFWorkbook(fileStream);
            }
            else if (extension == ".xls")
            {
                throw new DAOException(extension + " files are not supported. Please change them to .xlsx file format. \n Thanks");
            }
            else
            {
                throw new DAOException("File format " + extension + " not supported.");
            }
            ISheet sheet = null;

            if (sheet == null)
            {
                sheet = workbook.GetSheet("Invoice");
            }
            IRow  currRow;
            ICell cell;

            for (int i = 0, rowNum = 20; i < purchases.Count; i++)
            {
                Purchase p = purchases[i];
                currRow = sheet.GetRow(rowNum);
                cell    = currRow.GetCell(0);
                //SetValue(cell, "1", field);
                cell.SetCellValue(p.SNo);

                string[] strs = p.description.Split('\n');
                for (int j = 0; j < strs.Length; j++)
                {
                    currRow = sheet.GetRow(rowNum + j);
                    cell    = currRow.GetCell(1);
                    cell.SetCellValue(strs[j]);
                }

                currRow = sheet.GetRow(rowNum);
                cell    = currRow.GetCell(6);
                cell.SetCellValue(p.quantity);

                currRow = sheet.GetRow(rowNum);
                cell    = currRow.GetCell(8);
                cell.SetCellValue(p.amount);
                int count = p.description.Count(s => s == '\n') + 1;
                rowNum += count;
            }
            currRow = sheet.GetRow(40);
            cell    = currRow.GetCell(0);
            cell.SetCellValue(txtGSTAmountInWords.Text);

            currRow = sheet.GetRow(42);
            cell    = currRow.GetCell(0);
            cell.SetCellValue(txtTotalAmountInWords.Text);

            currRow = sheet.GetRow(38);
            cell    = currRow.GetCell(8);
            cell.SetCellValue(txtTotalAmount.Text);

            currRow = sheet.GetRow(39);
            cell    = currRow.GetCell(8);
            cell.SetCellValue(txtCGST.Text);

            currRow = sheet.GetRow(40);
            cell    = currRow.GetCell(8);
            cell.SetCellValue(txtSGST.Text);

            currRow = sheet.GetRow(41);
            cell    = currRow.GetCell(8);
            cell.SetCellValue(txtIGST.Text);

            currRow = sheet.GetRow(42);
            cell    = currRow.GetCell(8);
            cell.SetCellValue(txtGrandTotal.Text);

            OleDbConnection connection = DatabaseConnection.GetConnection();

            connection.Open();
            OleDbCommand command = new OleDbCommand("select * from qryDocumentFields where doctype_name=@DOCUMENT", connection);

            //Trace.WriteLine("Document Name " + documentName);
            command.Parameters.AddWithValue("@DOCUMENT", documentName);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                if (sheet == null)
                {
                    sheet = workbook.GetSheet(reader["field_sheet"].ToString());
                }
                currRow = sheet.GetRow(int.Parse(reader["field_row"].ToString()) - 1);
                cell    = currRow.GetCell(int.Parse(reader["field_column"].ToString()) - 1);
                //Trace.WriteLine("Field Name "+reader["field_name"]);
                SetValue(cell, reader["field_name"].ToString(), field);
            }


            using (var fileData = new FileStream(saveFileName, FileMode.Create))
            {
                workbook.Write(fileData);
                workbook.Close();
            }
        }