Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jun 15, 2023 6:41 am

Hi..

I have a question about the function of Spire.

I am working in the following this.

1. MS-Excel Open
2. Drag and select a specific range, and copy from Excel
3. Ms-Word Open
4. Paste into Ms-word

If I do this, the content copied from Excel will be pasted into Word while keeping format.
(border, font, color, width .... )

Can do this using spire.xls and spire.doc?.

Sungkyu_min
 
Posts: 36
Joined: Fri Jan 07, 2022 5:53 am

Thu Jun 15, 2023 8:41 am

Hi,

Thank you for your inquiry. You can copy the specified CellRange to a sheet then refer to our official website tutorial at the following link: https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Document-Operation/How-to-Export-Excel-Data-to-Word-Table-Maintaining-Formatting-in-C.html

Please find attached my testing code for your reference. However, please note that copying the CellRange will involve several details that may be unique to your document. Therefore, adjustments to the code may be necessary to suit your specific requirements and some needs may not be met.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Fri Jun 16, 2023 1:22 am

Hi..

I remembered that I had tried this before, but couldn't implement it.
When I tried this, the problem that occurred is as follows.

1. My sheet's LastColumn is 5, but Spire recognizes it as 7.

2. The cell's border type doesn't seem to be applied.
I added this code to the CopyStyle method, but it didn't worked.
Code: Select all
switch (xCell.Borders.Value) {
  case LineStyleType.None:
      wCell.CellFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
      break;
  case LineStyleType.Thin:
      wCell.CellFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.ThinThickSmallGap;
      break;
}


3. It seems that the width and height of the cells are not applied properly.
`table.Rows[r - 1].Height = (float)sheet.Rows[r - 1].RowHeight;` This code doesn't seem to work,
so I added this to the `CopyStyle` method.
But it still didn't worked.
Code: Select all
wCell.SetCellWidth( (float)xCell.ColumnWidth, CellWidthType.Auto);
wCell.OwnerRow.Height = (float)xCell.RowHeight;




I want to know why this code doesn't work.
There are Excel used for attachments, doc files created using spire, and files directly opened and pasted.

the Excel file may have a different display when the PC's default page is A4 and letter.
On my PC, A4 is the default page, and I have attached a screenshot when I open the Excel file.

Sungkyu_min
 
Posts: 36
Joined: Fri Jan 07, 2022 5:53 am

Fri Jun 16, 2023 9:11 am

Hi,

Thank you for your inquiry. Regarding your first question, I have analyzed your Excel document and found that the content range is A1:G30, as shown in the screenshot below. Since there are seven columns, the value of lastColumn is 7.
resolve.png

Regarding your second and third questions, I suggest trying to add simple Excel data to a Word document first. You can use debugging to view variable information .

Please note that our XLS and DOC products have different design structures, implementing this through code may not be as simple as dragging and dropping in the software. And due to different document structures, it can be challenging to provide a universal method for your needs.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc