telldurges says:
if
(row.RowIndex.Value == 1)
{
AddQuery +=
"IF OBJECT_ID('dbo."
+ map.FileName +
"', 'U') IS NULL "
;
AddQuery +=
"BEGIN "
;
AddQuery +=
"CREATE TABLE [dbo].["
+ map.FileName +
"]("
;
foreach
(Cell cell
in
row.Descendants<Cell>())
{
dt.Columns.Add(GetValue(doc, cell));
AddQuery +=
"["
+ GetValue(doc, cell).ToString() +
"]"
+
" VARCHAR(MAX),"
;
}
AddQuery = AddQuery.TrimEnd(
','
);
AddQuery +=
")"
;
AddQuery +=
" END"
;
}
Replace the above code with the below.
if (row.RowIndex.Value == 1)
{
AddQuery += "IF OBJECT_ID('dbo." + "Test" + "', 'U') IS NULL ";
AddQuery += "BEGIN ";
AddQuery += "CREATE TABLE [dbo].[" + "Test" + "](";
// AddQuery += "id int identity , ";
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Columns.Add(RemoveSpecialCharacters(GetValue(doc, cell)));
AddQuery += "[" + RemoveSpecialCharacters(GetValue(doc, cell)) + "]" + " VARCHAR(MAX),";
}
//firstRow = false;
AddQuery = AddQuery.TrimEnd(',');
AddQuery += ")";
AddQuery += " END";
}
For RemoveSpecialCharacters use the below method.
public string RemoveSpecialCharacters(string text)
{
return System.Text.RegularExpressions.Regex.Replace(text, @"(\s+|\*|\#|\@|\$)", "");
}
The query will be generated like below.
IF OBJECT_ID('dbo.Test', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[Test]
(
[InvoiceNumber] VARCHAR(MAX),
[P/OS] VARCHAR(MAX),
[InvoiceAmount] VARCHAR(MAX),
[TaxAmount] VARCHAR(MAX),
[DocType] VARCHAR(MAX)
)
END
Check the exmple.
HTML
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportExcel" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Namespaces
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Collections.Generic;
using System.Data;
Code
protected void ImportExcel(object sender, EventArgs e)
{
//Save the uploaded Excel file.
string filePath = @"C:\Users\Test\Desktop\Test.xlsx";
string AddQuery = "";
//Open the Excel file in Read Mode using OpenXml.
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePath, false))
{
//Read the first Sheets from Excel file.
Sheet sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
//Get the Worksheet instance.
Worksheet worksheet = (doc.WorkbookPart.GetPartById(sheet.Id.Value) as WorksheetPart).Worksheet;
//Fetch all the rows present in the Worksheet.
IEnumerable<Row> rows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();
//Create a new DataTable.
DataTable dt = new DataTable();
//Loop through the Worksheet rows.
foreach (Row row in rows)
{
//Use the first row to add columns to DataTable
if (row.RowIndex.Value == 1)
{
AddQuery += "IF OBJECT_ID('dbo." + "Test" + "', 'U') IS NULL ";
AddQuery += "BEGIN ";
AddQuery += "CREATE TABLE [dbo].[" + "Test" + "](";
// AddQuery += "id int identity , ";
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Columns.Add(RemoveSpecialCharacters(GetValue(doc, cell)));
AddQuery += "[" + RemoveSpecialCharacters(GetValue(doc, cell)) + "]" + " VARCHAR(MAX),";
}
//firstRow = false;
AddQuery = AddQuery.TrimEnd(',');
AddQuery += ")";
AddQuery += " END";
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Rows[dt.Rows.Count - 1][i] = GetValue(doc, cell);
i++;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private string GetValue(SpreadsheetDocument doc, Cell cell)
{
string value = cell.CellValue.InnerText;
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
return doc.WorkbookPart.SharedStringTablePart.SharedStringTable.ChildElements.GetItem(int.Parse(value)).InnerText;
}
return value;
}
public string RemoveSpecialCharacters(string text)
{
return System.Text.RegularExpressions.Regex.Replace(text, @"(\s+|\*|\#|\@|\$)", "");
}
Screenshot