Hi mod7609,
I have written code to read excel and word file and retrive the data to DataTable. Now you need to check the code and compare the datatable and based on your condition you need to write the code and create separete word file. Try this from your end and let me know. If any issue then revert me back.
C#
// Read table from word document and return DataTable
private DataTable ReadWord()
{
DataTable dt = new DataTable("Word");
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object filePath = Server.MapPath("~/Files/Test.rtf");
object missing = System.Type.Missing;
try
{
doc = word.Documents.Open(ref filePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
// Columns
for (int i = 1; i <= word.Application.ActiveDocument.Tables[1].Rows[1].Cells.Count; i++)
{
dt.Columns.Add(word.Application.ActiveDocument.Tables[1].Rows[1].Cells[i].Range.Text.Replace("\r\a", "").Trim());
}
// Rows
for (int i = 2; i < word.Application.ActiveDocument.Tables[1].Rows.Count; i++)
{
Word.Row tableRow = word.Application.ActiveDocument.Tables[1].Rows[i];
dt.Rows.Add(dt.NewRow());
for (int j = 1; j <= tableRow.Cells.Count; j++)
{
dt.Rows[i - 2][j - 1] = tableRow.Cells[j].Range.Text.Replace("\r\a", "").Trim();
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
((Word._Application)word).Quit();
}
return dt;
}
// Read excel file and return DataTable.
private DataTable ExcelToDataSet()
{
DataTable dt = new DataTable("Excel");
string filePath = Server.MapPath("~/Files/Test.xlsx");
string extension = Path.GetExtension(filePath);
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(1);
//Loop through the Worksheet rows.
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
//Use the first row to add columns to DataTable.
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
}
return dt;
}
}