Openxml excel c# does the row has merged cells - c#

I have a spreadsheet which has two rows with three columns,
--------------------------------------
| title | id | roles
------------------------------------------
| DOC | 1 | pediatrician
-----------------------------------------
| NURSE | 2 | A&E
| | | pediatrics
| | | orthopedic
-----------------------------------------
| DOC | 3 | orthopedic
-----------------------------------------
In which row 2 (NURSE ) column 3 (roles) have a merged cell
using C# (openxml) I want to read column (roles) as one single values
can some one help?

Related

SQL Get matching values from other table and show in main table

I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue

Search for last value in specific column

I am using Excel Interop in C# to read from a Excel Sheet.
My Excel Sheet has always the same Structure:
| A | B | C | D |
1 | | | | |
2 | Text | Text | Text | Text |
3 | | | | |
4 | Text | Text | Text | Text |
5 | Text | Text | Text | Text |
6 | Text | Text | Text | Text |
...| ... | ... | ... | ... |
75 | Text | Text | Text | Text |
76 | | | | |
77 | Text | | | |
78 | Text | | | |
79 | Text | | | |
What i now want to get, is that start and end row of my required data. In the example above it would be "4" for the start row and "75" for the end row.
The start row is no problem: i get it using Range.Find("text").Row because i know the first entry of my data.
The problem is getting line 75, because the function .End always gives me the very last cell of the sheet. This would be 79 in my example.
I could loop form row 4 to 76 and check if cell is empty but this does not seem to be very smart and efficient.
Is it possible to ask for example for the last not empty cell in column D? This would solve my problem.
Hope this was not too confusing...
Best Regards and thanks in advance,
Julian
"Is it possible to ask for example for the last not empty cell in column D? This would solve my problem."
Maybe this solution will be good for you. It finds first blank element, but I think you could edit it:
first blank row in excel

assign unique group id to related values between two columns

I have a challenging problem.I am developing a web app (.net(C#)) that takes an specific excel file and export an delimited file with specifications explained below.
Here is the excel to be imported:
Loan_ID |Owner_Id | RelPerson_Id | Loan_Class
------------------------------------------------
L131231 | 1234 | 5678 | 0
L135148 | 2345 | 6789 | 2
L417128 | 1234 | 5432 | 1
L271237 | 5678 | 1112 | 5
L213781 | 2345 | 1113 | 4
L098932 | 1117 | 6789 | 6
L012213 | 1122 | 3311 | 2
L398721 | 2221 | 1112 | 3
L098766 | 5552 | | 1
Desired output:
Cust_ID | Cust_class | Gr_ID | Gr.Members | Gr_Class
---------------------------------------------------------------------
1234 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
2345 | 4 | 2 | (2345,1117,6789,1113) | 6
5678 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1117 | 6 | 2 | (2345,1117,6789,1113) | 6
1122 | 2 | 3 | (1122,3311) | 2
6789 | 6 | 2 | (2345,1117,6789,1113) | 6
5432 | 1 | 1 | (1234,5678,5432,1112,2221) | 5
1112 | 5 | 1 | (1234,5678,5432,1112,2221) | 5
1113 | 4 | 2 | (2345,1117,6789,1113) | 6
3311 | 2 | 3 | (1122,3311) | 2
2221 | 3 | 1 | (1234,5678,5432,1112,2221) | 5
5552 | 1 | 4 | (5552) | 4
So,i will explain the case in order for you to understand.In the excel file, the first column represents the loan with unique id (no duplicates), the second column represent the Owner of the loan which is an id to identify the customer(Customer_Id).The id of Related person is also a Customer_Id which is the guarantor of the owner in this loan. A customer may have 2 or more loans and can be the guarantor in 2 or more other loans. The guarantor may be the guarantor in 2 or three loans but also may be the owner of another loan.So the owner_id and Rel_PersonId may have duplicates and the Rel_PersonId may have blank values . Class column is the rating or classification of the loan Id (Based on days in delay).
From this file i want the above output.
Cust_id is the distinct Customer Id which will be taken from both columns, Owner, and Rel Persons and will be assigned the highest class(Column :Cust_Class) of the loan he is part of(whether owner or guarantor).I already found a solution for the first two columns of the output, i am stuck with grouping.
Grouping logic:Two customers involved in one loan are grouped together, but if any of this customers is involved in another loan(wether like an Owner or a guarantor) with another Cutomer X, and in another with another customer Y both customers will be in a group with the first two, and if customer x or y are involved in other loans with other customers z and a those are added to the first group with the others too, and this cycle continues till there are no more customer connected with those that are already in the group.See the desired output for understanding.The group id will be generated automatically and sequentially(1,2,3,4) .The group class will be the class of the customer with the highest Cust_class of the group.
I am using sql server to import the excel.Any idea on how to build a query to gain this output or any other solutions?
Hope you can understand, feel free to ask for any further clarification.
Thank you.

MS SQL Table structure for dynamic app

I am building a dynamic app.
The basic of the app is i can create a question. A question may have many columns. These columns will be displayed in table structure. Then the user will fill the details. Below is the sample.
Question:
1. List the projects available and their deadlines?
Columns:
1. Project - Textbox
2. Deadline - Textbox
So i will show this data in the table like below.
The user will fill the row and click on submit button. Another empty row will get added to add the another response like below.
The user will submit any no of responses.
Like this the no of columns will be dynamic and also the each column will have some validation like Unique,AcceptsNull etc...
How to create table structure in ms sql server to save the question,column and the responses.
Please help thanks in advance..
Your scenario is similar with the way in which Microsoft Project Server stores its data in MSSQL. In MSPE for each project, a task can have a dynamic number of columns. In your case, if you can make all your column types as VARCHAR, than your scenario simplifies even more.
The proposed structure would be like this:
This is the table with the questions:
tbl_Questions
+------------------------------------------------------------------+
| Id | Question |
+----------------------------------------------------------------- +
| RandomGUID1 | "List the projects available and their deadlines?" |
| RandomGUID2 | "List the projects available?" |
+------------------------------------------------------------------+
tbl_RelationTable
+---------------------------------------------------------------------------+
| Id | QuestionId | Column1 | Column2 | ... |Column 1000 |
+---------------------------------------------------------------------------+
| 1 | RandomGUID1 | RandomGUID111 | RandomGUID112 | | null |
| 2 | RandomGUID2 | RandomGUID113 | null | | null |
+---------------------------------------------------------------------------+
In this table you store all relations between tbl_Questions and the other tables. You define in this table a sufficient number of columns, here I proposed 1000, but maybe in your case 10-15 columns would be enough. One important aspect is that you need to use GUIDs for Question ID in order to be unique.
Now we define the real data tables.
This is the table where you define your answer columns:
tbl_AnswerColumns
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | ColumnName |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | Title |
| 2| RandomGUID112 | RandomGUID1 | Answer |
| 3| RandomGUID113 | RandomGUID2 | Title |
+-----------------------------------------------+
This is the table where you store the answers values:
tbl_AnswerValues
+-----------------------------------------------+
|Id| RelationTableId | QuestionId | Answer |
+-----------------------------------------------+
| 1| RandomGUID111 | RandomGUID1 | "Answer1" |
| 2| RandomGUID112 | RandomGUID1 | "Answer2" |
| 3| RandomGUID113 | RandomGUID2 | "Answer3" |
+-----------------------------------------------+
You need to define foreign keys in all tables in order to make the data retrieval much faster. This is why each table should contain a foreign key to the QuestionId.
By the information you've given I'm thinking something like this?
Questions
+---------------------------------------------------------+
| Id | Question |
+----+--------------------------------------------------- +
| 1 | "List the projects available and their deadlines?" |
+---------------------------------------------------------+
Answers
+-----------------------------------------------------------------------------------+
| Id | QuestionId | Order | Title | Anwser | Unique | Nullable | Values |
+----+------------+-------+------------+-------------+--------+----------+----------+
| 1 | 1 | 0 | "Project" | "Project 1" | True | False | "P1, P2" |
| 2 | 1 | 1 | "Deadline" | "Apr-15" | False | True | NULL |
+-----------------------------------------------------------------------------------+
Edit: I've added the validations. If you want the same validations for all anwers for the same question, you can also move the validation columns to the Questions-table.

Creating editable table from multiple sql database tables in ASP.Net

I have a problem with displaying and editing some data from database to ASP.Net webpage.
Data from database consist from few different tables:
Table Files, witch contains file_id and a file name
| file_id | file_name |
--------------------------
| GUID | VARCHAR |
file is saved in database as a collection of lines for one file_id,
Table Items,witch contains basically all the lines from all the files
| item_id | file_id | step | number |
---------------------------------------------------
| GUID | VARCHAR | BIGINT | BIGINT |
Table Wording, witch contains all distinctive wording
| wording_id | wording_eng | description |
-----------------------------------------
| GUID | VARCHAR | VARCHAR |
And M_N_table witch contains connection between items and wording
| item_id | wording_id |
----------------------------
| GUID | GUID |
my final display table should look like this and should be editable
| | file1 | file2 | file3 |.........| fileN | | |
| sortorder --------------------------------------------------------------------------| wording_eng | desription |
| | step | number | step | number | step | number |.........| step | number | | |
------------------------------------------------------------------------------------------------------------------
| 1 | 10 | 15 | | | | |.........| | | something1 | something2 |
------------------------------------------------------------------------------------------------------------------
| 2 | | | | | 25 | 15 |.........| 5 | 17 | something3 | something4 |
------------------------------------------------------------------------------------------------------------------
| 3 | 2 | 4 | 5 | 16 | 17 | 3 |.........| 1 | 15 | something5 | something6 |
So for every file from some selected list,witch means variable number of files, I need to read all the wordings and if a wording is connected to item in M_N_table and that item exist in selected files I have to write value of step and number to that files column as shown, if they don't exist it will be written blank value.
Problem I'm having is creating Table or DataList or GridView where I can have connection to item_id and wording_id when I'm editing data. I managed to create "pivot" datatable in C# in backcode, using all of these tables and created gridview with dynamic number of columns but problem is I don't have information about what item_id I'm actually editing it just goes back to changing value in datatable.
What is the best way to display this data? I tried making dynamic pivot table in SQL to display it directly with gridview but without aggregation it's impossible,so I had to do it with datatable's in C# back-code. And how can I make it so that when I edit data in displayed table I get the same changes in database?

Categories