Change Excel sheet tab color using Open XML dll
Hi,
I want to change the sheet tab color of an excel Xlsx document. I am using the following code but it does not set the sheet color. I get object reference exception when I set the sheet tab color.
public static string filepath = @"C:\Test\Book1.xlsx";
private static void ChangeSheetcolor()
try
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filepath, false))
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
//my code
WorksheetPart worksheetPart =
GetWorksheetPartByName(spreadSheetDocument, "Sheet1");
if (worksheetPart != null)
// worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("Red");
worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("#CCCCCC");
// Save the worksheet.
worksheetPart.Worksheet.Save();
catch (Exception ex)
private static WorksheetPart
GetWorksheetPartByName(SpreadsheetDocument document,
string sheetName)
IEnumerable<Sheet> sheets =
document.WorkbookPart.Workbook.GetFirstChild<Sheets>().
Elements<Sheet>().Where(s => s.Name == sheetName);
if (sheets.Count() == 0)
//does not exist
return null;
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)
document.WorkbookPart.GetPartById(relationshipId);
return worksheetPart;
How to change the sheet tab color using Open XML dlls.
Thanks
Ashok
Hi J_Prasanna,
I'm afraid that it's not possible with OpenXML SDK, but it's possible if you use Excel PIA along with Internet Explorer. Use the Internet Explorer COM object to render the HTML content, then copy the document, use the Paste method of the Worksheet object
to paste the text with format.
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate "about:blank"
.document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
.document.body.createtextrange.execCommand "Copy"
ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1")
.Quit
End With
The code is similar if you use managed project.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.
Similar Messages
-
Change excel sheet name when using spool
Hi, all! I encounter a problem when I was trying to spool a sql to excel using sqlplus.
That is the sheet name is as same as the file name, but the requirement of client is the sheet name is "specific" and not as same as file name, could you please help if there is any solution for this.
thanks a lot for your help.hi
when you create XLS file manually in the command line, can you set the sheet's name?
- no
unless you use some "Basic's" commands to manipulate the file later within your batch file, I think there's no way (excel.exe file provides no options in the command line). -
Render HTML inside Excel, using Open XML
Hello guys,
I am creating excel file using, Open XML.
The data will be coming from database, and can contain HTML tags.
e.g. <b>some sample <i style='color:red'> text </i></b>
I want the HTML to be rendered inside excel sheet, so that user can view well formatted excel..
Please, Help me outHi J_Prasanna,
I'm afraid that it's not possible with OpenXML SDK, but it's possible if you use Excel PIA along with Internet Explorer. Use the Internet Explorer COM object to render the HTML content, then copy the document, use the Paste method of the Worksheet object
to paste the text with format.
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate "about:blank"
.document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
.document.body.createtextrange.execCommand "Copy"
ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1")
.Quit
End With
The code is similar if you use managed project.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey. -
When reading the Rich Text present in Excel column using open XML its taking it as plain text
Hello All,
When i am reading excel columns using open Xml in C# everything is working fine except the column that
contain Rich Text (ex: bold, italic,color,size). Its reading the Rich Text content as plain text. As we know for Rich Text in open XML we get the text as Runs (C#
object) which contains the text as value and rpr as run properties. I have also written the code to convert Runs to html. But the issue is that for some Rich Text format its reading it as Runs but most of the Rich Text its reading it as plain text.
The issue i am getting is, for some rich text its creating RUN and for some of the rich text its not creatingRUN.
Let me give the example for more understanding:
Suppose i have two cells in excel which contains the below text.
1. Rich Text
1
2. RichText
2
Now when i read these cells using the below code
var stringTable =spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (stringTable.SharedStringTable.ElementAt(index1).ChildElements.GetItem(0).GetType().Name == "Run")
Custom code to convert Rich text to HTML...
else
Read the plain text...
Though both the cell contains rich text, one is going in RUN block and one is going in Plain text bloc.
Also, one thing that i have noticed is when i use standard color for text its consider it as RUNS and when i use color apart from the standard color it does not consider it as RUNS. Same behavior occurs for different combination of text viz. bold, italic, underline
etcHi Ejaz,
This forum is for software developers who are using the Open Specification documentation to assist them in developing systems, services, and applications that are interoperable with Microsoft products. The Open Specifications can be found at:
http://msdn.microsoft.com/en-us/library/cc203350(PROT.10).aspx. Since your post does not appear to be related to the Open Specification documentation set, we would appreciate it if
you could try to post your question in a more relevant forum. Thank you.
Open XML Format SDK
https://social.msdn.microsoft.com/Forums/en-US/home?forum=oxmlsdk&filter=alltypes&sort=lastpostdesc
Josh Curry (jcurry) | Escalation Engineer | Open Specifications Support Team -
Auto fit columns in excel using open xml
I have generate excel using open xml and it contain auto fit property for columns. my problem is that the excel is opening very well in kingsoft spreadsheets
and other tools, but not opening in microsoft excel 2013 and older version. Is there any solution for auto fit columns in excel using open xml and should open in Microsoft excel all versionHi Raj12345,
>> I have generate excel using open xml and it contain auto fit property for columns. my problem is that the excel is opening very well in kingsoft spreadsheets and other tools, but not opening in microsoft excel 2013 and older version
I assume you was using OpenXML SDK and I'm afraid we are not able to find the root cause according to your description, would you mind sharing us some code sample to reproduce this issue.
>> Is there any solution for auto fit columns in excel using open xml and should open in Microsoft excel all version
As far as I know, there is no auto fit property for columns in excel with openxml. I am afraid that you need to calculate the column width depending on the cell contents, and then set the column width. The link below shows more details:
# Solution: How to AutoFit Excel Content
https://social.msdn.microsoft.com/Forums/office/en-US/28aae308-55cb-479f-9b58-d1797ed46a73/solution-how-to-autofit-excel-content?forum=oxmlsdk
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
In excel sheet tab name is not coming-urgent
hi all,
one small rewquirement. if u run this test program it opens a excel sheet which contains signle tab. here tab name is not coming. i dont no hot to display tabname here.anybody can make the changes and send me the code.
i am sending my code below.
thanks,
maheedhar.t
REPORT ytestvij MESSAGE-ID zv.
TABLES sscrfields.
TYPE-POOLS: icon.
TYPES : BEGIN OF zfnames_ds,
reptext TYPE reptext,
END OF zfnames_ds.
TYPE-POOLS ole2 .
DATA: wa_fntxt TYPE smp_dyntxt.
DATA : wa_t75_booking TYPE zvt75_booking_h,
t_t75_booking TYPE STANDARD TABLE OF zvt75_booking_h.
DATA : wa_fields TYPE dfies,
t_fields TYPE STANDARD TABLE OF dfies.
DATA : wa_fnames TYPE zfnames_ds,
t_fnames TYPE STANDARD TABLE OF zfnames_ds.
handles for OLE objects
DATA: h_excel TYPE ole2_object, " Excel object
h_mapl TYPE ole2_object, " list of workbooks
h_map TYPE ole2_object, " workbook
h_zl TYPE ole2_object, " cell
h_f TYPE ole2_object. " font
DATA h TYPE i.
DATA : lin TYPE i.
data: excel type ole2_object,
application type ole2_object,
books type ole2_object,
book type ole2_object,
sheet type ole2_object,
cell type ole2_object,
column type ole2_object.
PARAMETERS : p_input TYPE localfile.
Add button to application toolbar
SELECTION-SCREEN FUNCTION KEY 1. "Will have a function code of 'FC01'
INITIALIZATION.
Add displayed text string to buttons
wa_fntxt-icon_id = icon_xls.
wa_fntxt-icon_text = 'Input File template'.
wa_fntxt-quickinfo = 'T75 Header Data'.
sscrfields-functxt_01 = wa_fntxt.
AT SELECTION-SCREEN.
IF sscrfields-ucomm = 'FC01'.
do nothing
PERFORM open_excel.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_input.
PERFORM get_filename USING p_input.
START-OF-SELECTION.
*set pf-status 'ONE'.
END-OF-SELECTION.
WRITE : lin.
*& Form GET_FILENAME
text
--> p1 text
<-- p2 text
FORM get_filename USING p_file TYPE rlgrap-filename . "localfile.
DATA : w_rc TYPE i.
DATA : wa_file_table TYPE file_table ,
t_file_table TYPE STANDARD TABLE OF file_table.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
WINDOW_TITLE =
DEFAULT_EXTENSION =
DEFAULT_FILENAME =
FILE_FILTER =
INITIAL_DIRECTORY =
MULTISELECTION =
CHANGING
file_table = t_file_table[]
rc = w_rc
USER_ACTION =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
OTHERS = 4
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
LOOP AT t_file_table INTO wa_file_table.
p_file = wa_file_table-filename.
ENDLOOP.
ENDFORM. " GET_FILENAME
*& Form open_excel
text
--> p1 text
<-- p2 text
FORM open_excel.
SELECT * FROM ZVT75_BOOKING_H
INTO TABLE t_t75_booking
UP TO 10 ROWS.
start Excel
CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
PERFORM err_hdl.
SET PROPERTY OF h_excel 'Visible' = 1.
PERFORM err_hdl.
get list of workbooks, initially empty
CALL METHOD OF h_excel 'Workbooks' = h_mapl.
PERFORM err_hdl.
add a new workbook
CALL METHOD OF h_mapl 'Add' = h_map.
PERFORM err_hdl.
output column headings to active Excel sheet
PERFORM fill_cell USING 1 1 1 'Financial year'.
PERFORM fill_cell USING 1 2 1 'Financial quarter'.
PERFORM fill_cell USING 1 3 1 'Customer number'.
PERFORM fill_cell USING 1 4 1 'Booking Year'.
PERFORM fill_cell USING 1 5 1 'Financial quarter'.
PERFORM fill_cell USING 1 6 1 'Contract type'.
PERFORM fill_cell USING 1 7 1 'Sub Contract type'.
PERFORM fill_cell USING 1 8 1 'Customer purchase order number'.
PERFORM fill_cell USING 1 9 1 'Booking Amount'.
PERFORM fill_cell USING 1 10 1 'Currency Key'.
LOOP AT t_t75_booking into wa_t75_booking.
copy items to active EXCEL sheet
H = SY-TABIX + 1.
PERFORM FILL_CELL USING H 1 0 wa_t75_booking-BOOKYEAR.
PERFORM FILL_CELL USING H 2 0 wa_t75_booking-BOOKQTR.
PERFORM FILL_CELL USING H 3 0 wa_t75_booking-.
PERFORM FILL_CELL USING H 4 0 wa_t75_booking-BOOKYEAR.
PERFORM FILL_CELL USING H 5 0 wa_t75_booking-BOOKQTR.
ENDLOOP.
disconnect from Excel
FREE OBJECT h_excel.
PERFORM err_hdl.
ENDFORM. " open_excel
*& Form ERR_HDL
outputs OLE error if any *
--> p1 text
<-- p2 text
FORM err_hdl.
data test type sy-subrc.
test = sy-subrc.
IF test <> 0.
Message e000(ZV) with 'Error in OLE-Automation:'.
STOP.
ENDIF.
ENDFORM. " ERR_HDL
FORM FILL_CELL *
sets cell at coordinates i,j to value val boldtype bold *
FORM fill_cell USING i j bold val.
CALL METHOD OF h_excel 'Cells' = h_zl EXPORTING #1 = i #2 = j.
set property of sheet 'Name' = 'T75'.
PERFORM err_hdl.
SET PROPERTY OF h_zl 'Value' = val .
PERFORM err_hdl.
GET PROPERTY OF h_zl 'Font' = h_f.
PERFORM err_hdl.
SET PROPERTY OF h_f 'Bold' = bold .
PERFORM err_hdl.
ENDFORM.Hi,
Look at the below thread, i posted complete code in this one, just copy that Program and past in your SAP and run the Program, it will create 3 sheets with the names also, then look at the Sheet name in the code, you will understand where to add the code
Re: format an excel
Regards
Sudheer -
I want to download a report into Excel sheet with color Heading..Is it Poss
Hi All
I want to download error records into Excel sheet with color Heading..Is it Possible to download into excel with Color Heading?
here i am <b>using the 3 sheets in one</b>
t_error-bkpf -> Sheet1
t_error-bseg-> sheet 2
t-error-bsec -> sheet3.
Rgds
Raghav<b>The following thread has the code which will put data into multiple sheets</b>
Download to multiple sheets in Excel
FOR COLOR LOGIC JUST REFER THIS PROGRAM
*& Report ZNEGI17 *
REPORT ZNEGI17 NO STANDARD PAGE HEADING.
* this report demonstrates how to send some ABAP data to an
* EXCEL sheet using OLE automation.
INCLUDE OLE2INCL.
* handles for OLE objects
DATA: H_EXCEL TYPE OLE2_OBJECT, " Excel object
H_MAPL TYPE OLE2_OBJECT, " list of workbooks
H_MAP TYPE OLE2_OBJECT, " workbook
H_ZL TYPE OLE2_OBJECT, " cell
H_F TYPE OLE2_OBJECT. " font
TABLES: SPFLI.
DATA H TYPE I.
* table of flights
DATA: IT_SPFLI LIKE SPFLI OCCURS 10 WITH HEADER LINE.
*& Event START-OF-SELECTION
START-OF-SELECTION.
* read flights
SELECT * FROM SPFLI INTO TABLE IT_SPFLI UP TO 10 ROWS.
* display header
ULINE (61).
WRITE: / SY-VLINE NO-GAP,
(3) 'Flg'(001) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,
(4) 'Nr'(002) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,
(20) 'Von'(003) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,
(20) 'Nach'(004) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,
(8) 'Zeit'(005) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP.
ULINE /(61).
* display flights
LOOP AT IT_SPFLI.
WRITE: / SY-VLINE NO-GAP,
IT_SPFLI-CARRID COLOR COL_KEY NO-GAP, SY-VLINE NO-GAP,
IT_SPFLI-CONNID COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,
IT_SPFLI-CITYFROM COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,
IT_SPFLI-CITYTO COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,
IT_SPFLI-DEPTIME COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP.
ENDLOOP.
ULINE /(61).
* tell user what is going on
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-007
EXCEPTIONS
OTHERS = 1.
* start Excel
CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.
* PERFORM ERR_HDL.
SET PROPERTY OF H_EXCEL 'Visible' = 1.
* CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:kis_excel.xls'
* PERFORM ERR_HDL.
* tell user what is going on
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-008
EXCEPTIONS
OTHERS = 1.
* get list of workbooks, initially empty
CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.
PERFORM ERR_HDL.
* add a new workbook
CALL METHOD OF H_MAPL 'Add' = H_MAP.
PERFORM ERR_HDL.
* tell user what is going on
* CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
* EXPORTING
** PERCENTAGE = 0
* TEXT = TEXT-009
* EXCEPTIONS
* OTHERS = 1.
* output column headings to active Excel sheet
PERFORM FILL_CELL1 USING 1 1 1 'Flug'(001).
PERFORM FILL_CELL1 USING 1 2 0 'Nr'(002).
PERFORM FILL_CELL1 USING 1 3 1 'Von'(003).
PERFORM FILL_CELL1 USING 1 4 1 'Nach'(004).
PERFORM FILL_CELL1 USING 1 5 1 'Zeit'(005).
LOOP AT IT_SPFLI.
* copy flights to active EXCEL sheet
H = SY-TABIX + 1.
PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.
PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.
PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.
PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.
PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.
ENDLOOP.
* changes by Kishore - start
* CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.
CALL METHOD OF H_EXCEL 'Worksheets' = H_MAPL." EXPORTING #1 = 2.
PERFORM ERR_HDL.
* add a new workbook
CALL METHOD OF H_MAPL 'Add' = H_MAP EXPORTING #1 = 2.
PERFORM ERR_HDL.
* tell user what is going on
SET PROPERTY OF H_MAP 'NAME' = 'COPY'.
* CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
* EXPORTING
** PERCENTAGE = 0
* TEXT = TEXT-009
* EXCEPTIONS
* OTHERS = 1.
* output column headings to active Excel sheet
PERFORM FILL_CELL1 USING 1 1 1 'Flug'(001).
PERFORM FILL_CELL1 USING 1 2 0 'Nr'(002).
PERFORM FILL_CELL1 USING 1 3 1 'Von'(003).
PERFORM FILL_CELL1 USING 1 4 1 'Nach'(004).
PERFORM FILL_CELL1 USING 1 5 1 'Zeit'(005).
LOOP AT IT_SPFLI.
* copy flights to active EXCEL sheet
H = SY-TABIX + 1.
PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.
PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.
PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.
PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.
PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.
ENDLOOP.
* changes by Kishore - end
* disconnect from Excel
* CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'C:SKV.XLS'.
FREE OBJECT H_EXCEL.
PERFORM ERR_HDL.
* FORM FILL_CELL *
* sets cell at coordinates i,j to value val boldtype bold *
FORM FILL_CELL1 USING I J BOLD VAL.
data : color(5) type x value 'H80000008'.
CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.
PERFORM ERR_HDL.
SET PROPERTY OF H_ZL 'Value' = VAL .
PERFORM ERR_HDL.
GET PROPERTY OF H_ZL 'Font' = H_F.
PERFORM ERR_HDL.
SET PROPERTY OF H_F 'Bold' = BOLD .
PERFORM ERR_HDL.
SET PROPERTY OF H_F 'ColorIndex' = 3 .
PERFORM ERR_HDL.
ENDFORM.
*& Form ERR_HDL
* outputs OLE error if any *
* --> p1 text
* <-- p2 text
FORM ERR_HDL.
IF SY-SUBRC <> 0.
WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.
STOP.
ENDIF.
ENDFORM. " ERR_HDL
*& Form FILL_CELL1
* text
* -->P_H text
* -->P_1 text
* -->P_0 text
* -->P_IT_SPFLI_CARRID text
form FILL_CELL using I J BOLD VAL.
CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.
PERFORM ERR_HDL.
SET PROPERTY OF H_ZL 'Value' = VAL .
PERFORM ERR_HDL.
GET PROPERTY OF H_ZL 'Font' = H_F.
PERFORM ERR_HDL.
endform. " FILL_CELL1
kishan negi -
Hi Friends,
Instead of display Sheet 1 in the Excel sheet tab(Bex), is there a way we can display Query Name in the excel sheet tab? please let me know.
Thanks,
KKHi Kumar
Can you please share how it was solved ?
Ashish -
How to change foreground and background color using CC SDK?
Hello all,
I am just starting to study the sdk (Mac CC). Currently, I am seeking the answer to change foreground and background color using CC SDK?
Is it possible to do so? In addition, will the Image Mode such as RGB, CYMK or Grayscale be effecting such solution?
Thanks in advance.These are NOT dropdown menus. They are dropdown LISTS.
http://web.me.com/toad.hall/Demo1/Drop_DownMenus.html
And from the links in the Google search you can learn that a list item has no style.
And a dropdown list behaves differently from dropdown menus. Here's how it looks on Mobile Safari : -
Excel sheet attached mail when opened shows a error message
hi all,
i have created a report for sending the output to the sap inbox as excel attachment . my problem is ;it always shows an error message when ever the attachment is opened.
I have used the function 'SO_NEW_DOCUMENT_ATT_SEND_API1' and in the table parameters, TABLES
packing_list =
passing the .XLS format and excel sheet name.
please provide me a solution to stop the display of this error message having the contents 'this file is not in a recognizable format'.
thanks and regards\
Thomas
Coding :
FORM sub_output_timeadm USING lv_usrid TYPE gty_timhours-usrid.
DATA : lv_emp1(10) TYPE c,
lv_ename(40) TYPE c,
lv_hour(10) ,
lv_lgart(15),
lv_scc(10),
lv_rcc(10),
lv_order(15),
lv_ordtext(40),
lv_act(10).
DATA : lwa_content TYPE solisti1.
DATA : lit_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE,"#EC *
lit_objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
lit_objtxt LIKE solisti1 OCCURS 2 WITH HEADER LINE,
lit_reclist LIKE somlreci1 OCCURS 2 WITH HEADER LINE,
lwa_docdat LIKE sodocchgi1 OCCURS 1 WITH HEADER LINE,
lv_tab_lines LIKE sy-tabix.
DATA : lv_file LIKE rlgrap-filename,
lv_line LIKE solisti1-line,
lv_vt TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
DATA : lwa_receivers TYPE somlreci1 .
*Object text details
PERFORM sub_objtxt_data TABLES lit_objtxt
lit_objpack
USING lv_tab_lines.
*Document data details
PERFORM sub_document_data TABLES lwa_docdat
lit_objtxt
USING lv_file
lv_tab_lines.
PERFORM sub_topofpage TABLES lit_objbin
USING lv_line
lv_vt.
PERFORM sub_fill_columnhead TABLES lit_objbin.
LOOP AT git_content INTO lwa_content.
lv_emp1 = lwa_content+2(10).
CONDENSE lv_emp1.
lv_ename = lwa_content+15(40).
SHIFT lv_ename LEFT.
* CONDENSE lv_ename.
IF lv_ename+0(7) = text-024.
WRITE lv_ename TO lv_ename RIGHT-JUSTIFIED.
ELSE.
CONDENSE lv_ename.
ENDIF.
lv_hour = lwa_content+58(10).
CONDENSE lv_hour.
lv_lgart = lwa_content+70(15).
CONDENSE lv_lgart.
lv_scc = lwa_content+90(10).
CONDENSE lv_scc.
lv_rcc = lwa_content+103(10).
CONDENSE lv_rcc.
lv_order = lwa_content+115(15).
CONDENSE lv_order.
lv_ordtext = lwa_content+132(40).
CONDENSE lv_ordtext.
lv_act = lwa_content+174(10).
CONDENSE lv_act.
CONCATENATE lv_emp1
lv_ename
lv_hour
lv_lgart
lv_scc
lv_rcc
lv_order
lv_ordtext
lv_act INTO lv_line SEPARATED BY lv_vt.
CONCATENATE gc_add lv_line lv_vt INTO lv_line.
APPEND lv_line TO lit_objbin.
CLEAR :lv_emp1,
lv_ename,
lv_hour,
lv_lgart,
lv_scc,
lv_rcc,
lv_order,
lv_ordtext,
lv_act ,lv_line,lwa_content.
ENDLOOP.
DESCRIBE TABLE lit_objbin LINES lv_tab_lines.
lv_tab_lines = lv_tab_lines + 1.
* Object pack details
PERFORM sub_objpack_data TABLES lit_objbin
lit_objpack
USING lv_tab_lines
lv_file.
*Receiver list details
PERFORM sub_reclist_timeadm TABLES lit_reclist
USING lv_usrid.
READ TABLE lit_reclist INDEX 1.
IF sy-subrc = 0.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = lwa_docdat
put_in_outbox = gc_x
commit_work = gc_x
TABLES
packing_list = lit_objpack
contents_bin = lit_objbin
contents_txt = lit_objtxt
receivers = lit_reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
CASE sy-subrc.
* WHEN 0.
* MESSAGE i014.
* WHEN 1.
* MESSAGE i016.
* WHEN 2.
* MESSAGE i016.
* WHEN 4.
* MESSAGE i015.
* WHEN OTHERS.
* MESSAGE i017.
ENDCASE.
ENDIF.
REFRESH git_content.
CLEAR gwa_content.
FORM sub_objtxt_data TABLES xt_objtxt STRUCTURE solisti1
xt_objpack STRUCTURE sopcklsti1
USING xv_tab_lines .
DATA : lv_zdat(10) TYPE c,
lv_zdat2(10) TYPE c.
CONSTANTS: lc_hiphen(1) TYPE c VALUE '-'.
* write sy-datum to lv_zdat mm/dd/yyyy.
* concatenate text-022 lv_zdat into xt_objtxt separated by space.
* APPEND xt_objtxt.clear xt_objtxt.
WRITE pnpbegda TO lv_zdat MM/DD/YYYY.
WRITE pnpendda TO lv_zdat2 MM/DD/YYYY.
CONCATENATE text-080
lv_zdat
lc_hiphen
lv_zdat2 INTO xt_objtxt SEPARATED BY space.
APPEND xt_objtxt.CLEAR xt_objtxt.
xt_objtxt = text-067.
APPEND xt_objtxt.CLEAR xt_objtxt.
CLEAR: lv_zdat,
lv_zdat2.
DESCRIBE TABLE xt_objtxt LINES xv_tab_lines.
READ TABLE xt_objtxt INDEX xv_tab_lines.
CLEAR xt_objpack-transf_bin.
xt_objpack-head_start = 1.
xt_objpack-head_num = 0.
xt_objpack-body_start = 1.
xt_objpack-body_num = xv_tab_lines.
xt_objpack-doc_type = gc_raw.
APPEND xt_objpack.
CLEAR xt_objpack.
ENDFORM. " sub_objtxt_data
*& Form sub_document_data
*To fix the size of the document
* -->XT_DOCDAT
* -->XT_OBJTXT
* -->XV_LV_FILE
* -->XV_TAB_LINES
FORM sub_document_data TABLES xt_docdat STRUCTURE sodocchgi1
xt_objtxt STRUCTURE solisti1
USING xv_lv_file
xv_tab_lines. "#EC *
DATA : lv_subject TYPE string,
lv_open TYPE c VALUE '(',
lv_close TYPE c VALUE ')',
lv_slash TYPE c VALUE '/'.
xt_docdat-obj_name = xv_lv_file.
CONCATENATE lv_open sy-sysid
lv_slash sy-mandt lv_close INTO lv_subject.
xt_docdat-obj_descr = lv_subject.
xt_docdat-doc_size
= ( xv_tab_lines - 1 ) * 255 + STRLEN( xt_objtxt ).
ENDFORM. " sub_document_data
*& Form sub_topofpage
* text
* -->P_LIT_OBJBIN text
FORM sub_topofpage TABLES xt_objbin STRUCTURE solisti1
USING xv_lv_line
xv_vt.
DATA: lwa_line TYPE slis_listheader.
LOOP AT git_header INTO lwa_line.
CONCATENATE lwa_line-key lwa_line-info
INTO xv_lv_line SEPARATED BY xv_vt.
CONCATENATE gc_add xv_lv_line xv_vt
xv_vt "TAB space
xv_vt
xv_vt
xv_vt
xv_vt
xv_vt
* xv_vt
* xv_vt
* xv_vt
* xv_vt
* xv_vt
* xv_vt
* xv_vt
INTO xv_lv_line.
APPEND xv_lv_line TO xt_objbin.
ENDLOOP.
ENDFORM. " sub_topofpage
*& Form sub_fill_columnhead
* text
* --> p1 text
* <-- p2 text
FORM sub_fill_columnhead TABLES xt_objbin STRUCTURE solisti1.
DATA: lv_line LIKE solisti1-line,
lv_vt TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
lv_zdat(10).
DATA : lv_line1(40),
lv_line2(10),
lv_line3(10),
lv_line4(3),
lv_line5(40).
lv_line1 = text-008.
WRITE sy-datum TO lv_zdat MM/DD/YYYY.
lv_line2 = lv_zdat.
CONCATENATE lv_vt
lv_line1
lv_vt
lv_vt
lv_line2
lv_vt
lv_vt
lv_vt
lv_vt
* lv_vt
INTO lv_line.
CONCATENATE gc_add lv_line lv_vt INTO lv_line.
APPEND lv_line TO xt_objbin.
CLEAR : lv_line, lv_line1, lv_line2, lv_zdat.
lv_line1 = text-009.
lv_line2 = text-010.
WRITE pnpbegda TO lv_zdat MM/DD/YYYY.
lv_line3 = lv_zdat.
CLEAR lv_zdat.
lv_line4 = ' - '.
WRITE pnpendda TO lv_zdat MM/DD/YYYY.
lv_line5 = lv_zdat.
CONCATENATE lv_vt
lv_line1
lv_vt
lv_vt
lv_line2
lv_vt
lv_line3
lv_line4
lv_line5
lv_vt
lv_vt
lv_vt
lv_vt
INTO lv_line.
CONCATENATE gc_add lv_line lv_vt INTO lv_line.
APPEND lv_line TO xt_objbin.
CLEAR : lv_line, lv_line1,
lv_line2,lv_line3,
lv_line4, lv_line5, lv_zdat.
CONCATENATE lv_vt
lv_vt
lv_vt
lv_vt
lv_vt
lv_vt
lv_vt
lv_vt
lv_vt
INTO lv_line.
CONCATENATE gc_add lv_line lv_vt INTO lv_line.
APPEND lv_line TO xt_objbin.
CLEAR lv_line.
CONCATENATE text-013
text-014
text-015
text-016
text-017
text-018
text-019
text-026
text-020
INTO lv_line
SEPARATED BY lv_vt.
CONCATENATE gc_add lv_line
lv_vt "TAB space
INTO lv_line.
APPEND lv_line TO xt_objbin.
ENDFORM. " sub_fill_columnhead
*& Form sub_objpack_data
*To fix the actual document size
* -->XT_OBJBIN
* -->XT_OBJPACK
* -->XV_TAB_LINES
* -->XV_LV_FILE
FORM sub_objpack_data TABLES xt_objbin STRUCTURE solisti1
xt_objpack STRUCTURE sopcklsti1
USING xv_tab_lines
xv_lv_file. "#EC *
DATA : lwa_objpack TYPE sopcklsti1.
lwa_objpack-transf_bin = gc_x.
lwa_objpack-head_start = 1.
lwa_objpack-head_num = 0.
lwa_objpack-body_start = 1.
lwa_objpack-body_num = xv_tab_lines.
lwa_objpack-doc_type = gc_xls.
lwa_objpack-obj_name = xv_lv_file.
lwa_objpack-obj_descr = gc_descr.
lwa_objpack-doc_size
= ( xv_tab_lines - 1 ) * 255 + STRLEN( xt_objbin ).
APPEND lwa_objpack TO xt_objpack.
CLEAR lwa_objpack.
ENDFORM. " sub_objpack_data
*& Form sub_reclist_timeadm
* text
* -->P_LIT_RECLIST text
* -->P_LV_USRID text
FORM sub_reclist_timeadm TABLES xt_reclist STRUCTURE somlreci1
USING lp_usrid TYPE gty_timhours-usrid."#EC *
*local constant
CONSTANTS : lc_b TYPE c VALUE 'B'.
*local workarea.
DATA : lwa_receivers TYPE somlreci1 ,
lwa_usrid TYPE gty_usrid ,
lwa_docdata TYPE sodocchgi1.
REFRESH : git_receivers.":, LIT_DOCDATA .
CLEAR : lwa_receivers,lwa_docdata.
IF p_timadm EQ 'X'.
MOVE: lp_usrid TO lwa_receivers-receiver,
text-002 TO lwa_receivers-express,
lc_b TO lwa_receivers-rec_type.
APPEND lwa_receivers TO xt_reclist.
ELSEIF p_manger EQ 'X'.
READ TABLE git_usrid INTO lwa_usrid WITH KEY pernr = gwa_emphours-magpernr
usrty = '0001' BINARY SEARCH.
IF sy-subrc EQ 0.
MOVE: lwa_usrid-usrid TO lwa_receivers-receiver,
text-002 TO lwa_receivers-express,
lc_b TO lwa_receivers-rec_type.
APPEND lwa_receivers TO xt_reclist.
ENDIF.
CLEAR : lwa_usrid .
ENDIF.Hi Tona,
Please go through the code and cross check....
REPORT zcyborg_5 NO STANDARD PAGE HEADING.
TABLES : eket.
DATA : p_date TYPE d.
DATA : w_lines TYPE sy-tabix.
TYPES : BEGIN OF types_eket,
ebeln TYPE eket-ebeln,
ebelp TYPE eket-ebelp,
eindt TYPE eket-eindt,
END OF types_eket.
DATA : t_eket TYPE STANDARD TABLE OF types_eket.
Declaration for ALV display *********************
DATA : w_grid TYPE REF TO cl_gui_alv_grid,
w_custom_container TYPE REF TO cl_gui_custom_container.
RANGES : r_eindt FOR eket-eindt.
FIELD-SYMBOLS: <fs_eket> TYPE types_eket.
INITIALIZATION.
CALL FUNCTION 'CALCULATE_DATE'
EXPORTING
days = '60-'
months = '0'
start_date = sy-datum
IMPORTING
result_date = p_date.
r_eindt-low = p_date.
r_eindt-high = sy-datum.
r_eindt-sign = 'I'.
r_eindt-option = 'BT'.
APPEND r_eindt.
START-OF-SELECTION.
PERFORM get_data.
PERFORM disp_data.
PERFORM email_data.
perform disp_alv.
*& Form get_data
text
--> p1 text
<-- p2 text
FORM get_data.
SELECT ebeln
ebelp
eindt
FROM eket
INTO TABLE t_eket
WHERE eindt IN r_eindt.
ENDFORM. " get_data
*& Form disp_data
text
--> p1 text
<-- p2 text
FORM disp_data.
DESCRIBE TABLE t_eket LINES w_lines.
WRITE :/5 'No of rows : ', w_lines.
WRITE :/5 'EBELN', 30 'EBELP', 50 'EINDT'.
SORT t_eket DESCENDING BY eindt.
LOOP AT t_eket ASSIGNING <fs_eket>.
AT NEW ebeln.
ULINE.
ENDAT.
WRITE : /5 <fs_eket>-ebeln, 30 <fs_eket>-ebelp, 50 <fs_eket>-eindt.
ENDLOOP.
ENDFORM. " disp_data
call function 'SEND_TABLE_TO_EXCEL'
exporting
structure_name = 'EKET'
tables
exceltab = t_eket
FORM email_data.
*Structures and internal table used to send data vai mail
DATA : objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE,
objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE,
objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE,
tab_lines LIKE sy-tabix,
doc_chng LIKE sodocchgi1.
DATA : it_attach TYPE STANDARD TABLE OF solisti1,
w_attach TYPE solisti1.
CONSTANTS : c_car TYPE x VALUE '0D',
c_tab TYPE x VALUE '09'.
CONCATENATE 'EBELN'
c_tab
'EBELP'
c_tab
'EINDT'
c_car INTO w_attach.
APPEND w_attach TO it_attach.
LOOP AT t_eket INTO <fs_eket>.
CONCATENATE <fs_eket>-ebeln
c_tab
<fs_eket>-ebelp
c_tab
<fs_eket>-eindt
c_car INTO w_attach.
APPEND w_attach TO it_attach.
ENDLOOP.
MOVE it_attach[] TO objbin[].
*assign a name to the email document
doc_chng-obj_name = 'Output'.
doc_chng-obj_descr = 'Old Scheduling lines'."gives the subject line
objtxt-line = 'Regards'.
APPEND objtxt.
objtxt-line = 'Shivaji'.
APPEND objtxt.
*determine the size of the body of the email
DESCRIBE TABLE objtxt LINES tab_lines.
READ TABLE objtxt INDEX tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
*fill the fields of the packing list for the body of the email
*doc needs no header
objpack-head_start = 1.
objpack-head_num = 0.
*but it has a body
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
APPEND objpack.
*************fill the fields for packing list ***
for attachment ********
DESCRIBE TABLE objbin LINES tab_lines.
*************if it has no header ****************
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 1.
*************if it has a body ********
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'XLS'.
objpack-obj_name = 'Attachement'.
objpack-obj_descr = 'file'.
objpack-doc_size = tab_lines * 255.
APPEND objpack.
************fill the mail recipient list*******
reclist-receiver = sy-uname.
**rec_type 'B' indicates sap user ,rec_type 'U' indicates internet user
reclist-rec_type = 'B'.
reclist-express = 'X'.
APPEND reclist.
Function Module to send the data as an excel attachement ****
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = doc_chng
put_in_outbox = 'X'
IMPORTING
SENT_TO_ALL =
NEW_OBJECT_ID =
TABLES
packing_list = objpack
OBJECT_HEADER =
contents_bin = objbin
contents_txt = objtxt
CONTENTS_HEX =
OBJECT_PARA =
OBJECT_PARB =
receivers = reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. -
Data in excel sheet to be used for programing
Hi,
I have to make a z report for which the data i have to use is in a excel sheet.
How can I use this data for making different reports.
Reg,
ArchanaHi,
Try the following code for multiple sheets.
data : $i_intern type kcde_cells occurs 0 with header line.
data : $v_index type i.
data : $v_start_col type i value '1',
$v_start_row type i value '1',
$v_end_col type i value '256',
$v_end_row type i value '7500'.
data: excel_tab type kcde_sender.
data: separator type c.
field-symbols: <field>.
data: application type ole2_object,
workbook type ole2_object,
range type ole2_object,
worksheet type ole2_object,
worksheets type ole2_object,
sheets type ole2_object.
data: h_cell type ole2_object.
data: h_cell1 type ole2_object.
data: l_sheet type c length 40.
data: l_active_sheet type i.
define m_message.
case sy-subrc.
when 0.
when 1.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
when others. raise upload_ole.
endcase.
end-of-definition.
Create Excel OLE2 object and open XLS file
if application-header = space or application-handle = -1.
create object application 'Excel.Application'.
m_message.
endif.
call method of application 'Workbooks' = workbook.
m_message.
call method of workbook 'Open' exporting #1 = pa_file.
m_message.
Show/don't show XLS
set property of application 'Visible' = 1.
m_message.
Determine number of sheets
call method of application 'Sheets' = sheets.
m_message.
call method of sheets 'Count' = sheetno.
m_message.
l_active_sheet = 0.
do sheetno times.
clear: $i_intern[], excel_tab[].
clear: it_vals[], it_chars[].
l_active_sheet = l_active_sheet + 1.
Activate sheet number L_ACTIVE_SHEET
call method of application 'Worksheets' = worksheets exporting #1 = l_active_sheet.
m_message.
call method of worksheets 'Activate'.
m_message.
Get active sheet
get property of application 'ACTIVESHEET' = worksheet.
m_message.
Find start
call method of worksheet 'Cells' = h_cell
exporting #1 = $v_start_row #2 = $v_start_col.
m_message.
Find end
call method of worksheet 'Cells' = h_cell1
exporting #1 = $v_end_row #2 = $v_end_col.
m_message.
Create range
call method of worksheet 'RANGE' = range
exporting #1 = h_cell #2 = h_cell1.
m_message.
Select range
call method of range 'SELECT'.
m_message.
copy to Clipboard
call method of range 'COPY'.
m_message.
call function 'CONTROL_FLUSH'
exceptions
others = 3.
Import clipboard
call function 'CLPB_IMPORT'
tables
data_tab = excel_tab
exceptions
clpb_error = 1
others = 2.
if sy-subrc <> 0. message x001(kx). endif.
separator = cl_abap_char_utilities=>horizontal_tab.
perform separated_to_intern_convert(saplkcde) tables excel_tab $i_intern
using separator.
set property of application 'CutCopyMode' = 0.
m_message.
enddo.
call method of application 'QUIT'.
m_message.
free object : application,
workbook,
worksheet,
sheets,
range.
m_message.
Hope it works.
Please reward accordingly.
Thanks,
Priyanka -
How to change the Hyper links color in the XML form
HI SDNs
Do u have any idea how to change the hyperlink color in the xml form. My requirement is as follows.I have created xml form by using xml template and i displayed this xml form by using Km document iview. I need tochange the hyper link color in the xml form content. I dont want to go for manual process by editing the xml form. because i have several hundreds of xml forms.
Appreciate if you give suggestion.
Thanks
RamanaHi Ramana,
The most convenient way to change the hyper links is by using the Theme Editor. Go to System Administration --> Portal Display --> Select Theme --> Component-Specific Styles --> Links --> Function/Result Link.
Hope this helps.
Kind regards,
Joost -
Read Excel sheet in KM using API.
Hi all,
I kept one Excel sheet (Xsl file) in KM. I want to read that Excel (Xsl) file by using API in NWDS.
How to read ? Is this possible
Helpful answer will be appreciate
Thanks & Regards
Thillai JHi Thillai
It is possible to read the files using any of the Java Excel API's ( like POI or JExcel or JDBC-ODBC bridge). It depends on your convenience and requirement as to which one to choose for.
[POI|http://poi.apache.org/] is seem to be widely used.
In your case, the sequence of actions will be as follows:
1. Access the resource and get the content.
2. Get an InputStream from the content object.
3. Next, using this InputStream, the data can be read.
4. Refer to [this|http://www.javabeat.net/articles/41-apache-poi-reading-excel-sheet-using-java-1.html] link as to how read data. (It is assumed that you have successfully retrieved the InputStream in Step 2)
Hope that helps.
Thanks
Deepak -
How to import tablesfrom excel sheet to database using sqldeveloper
hi dear,
i have lots of tables without data in excell sheet. i want to import all the tables in to data base using sql developer .how to import pls help meNo one here is your 'dear' so please don't use such terms.
Whenever you post provide your sql developer version.
>
i have lots of tables without data in excell sheet. i want to import all the tables in to data base using sql developer
>
You will need to provide an example of what you mean by a table without data.
What database and what version. What do you expect to end up with in the database after such an 'import'? -
Insert a Picture into the Picture/Image Content Control using Open XML SDK 2.0
I have created a word template as follows
Here PizzaPicture is a Picture Content Control, PizzaName is a Plain Text Content Control and PizzaDescription is a Rich Text Content Control
I am able to assign text to PizzaName and PizzaDescription Easily but not able to even locate the PizzaPicture. Here is my code
using (WordprocessingDocument document = WordprocessingDocument.Open(@".\test.docx", true)) {
MainDocumentPart mainPart = document.MainDocumentPart;
DocumentFormat.OpenXml.Wordprocessing.Text text = null;
SdtContentBlock pizzaNameBlock = null;
SdtContentBlock pizzaDescriptionBlock = null;
//SdtContentBlock pizzaPictureBlock = null;
List<SdtBlock> sdtList = mainPart.Document.Descendants<SdtBlock>().ToList();
foreach (SdtBlock sdt in sdtList)
Console.WriteLine(sdt.SdtProperties.GetFirstChild<Tag>().Val.Value);
SdtBlock s1 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaName").Single();
SdtBlock s2 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaDescription").Single();
SdtBlock s3 = mainPart.Document.Body.Descendants<SdtBlock>().FirstOrDefault(r =>
SdtProperties p = r.Elements<SdtProperties>().FirstOrDefault();
if (p != null)
Console.WriteLine("P is not null");
// Is it a picture content control?
SdtContentPicture pict =
p.Elements<SdtContentPicture>().FirstOrDefault();
if (pict != null) Console.WriteLine("Pict is not null");
// Get the alias.
SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();
if (pict != null && a.Val == "PizzaPicture")
return true;
return false;
if (s3 == null) {
Console.Write(" s3 is Null");
} else {
Console.WriteLine("s3 not null");
if (s1 != null) {
pizzaNameBlock = s1.Descendants<SdtContentBlock>().FirstOrDefault();
text = pizzaNameBlock.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault();
// here you can set the current time
text.Text = "Peperoni";
Console.WriteLine(text.Text);
string embed = null;
if (s3 != null)
Drawing dr = s3.Descendants<Drawing>().FirstOrDefault();
if (dr != null)
Blip blip = dr.Descendants<Blip>().FirstOrDefault();
if (blip != null)
embed = blip.Embed;
if (embed != null)
IdPartPair idpp = document.MainDocumentPart.Parts
.Where(pa => pa.RelationshipId == embed).FirstOrDefault();
if (idpp != null)
ImagePart ip = (ImagePart)idpp.OpenXmlPart;
using (FileStream fileStream =
File.Open(@"c:\temp\pepperoni.jpg", FileMode.Open))
ip.FeedData(fileStream);
if (s2 != null) {
pizzaDescriptionBlock = s2.Descendants<SdtContentBlock>().FirstOrDefault();
string altChunkId = "AltChunkId12345";
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId);
chunk.FeedData(File.Open(@".\html.txt", FileMode.Open));
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
////Replace content control with altChunk information
OpenXmlElement parent = pizzaDescriptionBlock.Parent;
parent.InsertAfter(altChunk, pizzaDescriptionBlock);
pizzaDescriptionBlock.Remove();
mainPart.Document.Save();
document.Close();
Here the call mainPart.Document.Descendants<SdtBlock>().ToList(); returns me only 2 items PizzaName, PizzaDescription but
no PizzaPicture.
Secondly the line SdtContentPicture pict = p.Elements<SdtContentPicture>().FirstOrDefault();
returns NULL. So it seems that there is no element of type SdtContentPicture.
What am I doing wrong? I am pretty sure that I have put PizzaPicture as a Picture content control.
My objective is to locate this control in the document and then insert an image into it programmatically.
MSDNStudent Knows not much!Hi,
I'm running some test on your code, and I'll come back as soon as I get some update. But before that, I would recommend you to bind customXMLPart with the content controls via Content Control Tool Kit,
which enables us to bind customXMLPart with CC by dragging and dropping.
After binding, you can change contents of the template via OpenXML SDK easily as the following code:
private void button1_Click(object sender, EventArgs e)
string fileName = @"C:\CCTest.docx";
WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true);
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
CustomXmlPart customPart = mainPart.CustomXmlParts.FirstOrDefault();
//convert image into string
string picName = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
System.IO.FileStream fileStream = System.IO.File.Open(picName, System.IO.FileMode.Open);
System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream);
byte[] byteArea;
byteArea = br.ReadBytes(System.Convert.ToInt32(fileStream.Length));
string picString = System.Convert.ToBase64String(byteArea);
//Load the XML template
string DataString = Properties.Resources.XMLData;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(DataString);
//change the value
XmlNodeList xmlNode = xmlDoc.GetElementsByTagName("pizzaPic");
xmlNode[0].InnerText = picString;
xmlNode = xmlDoc.GetElementsByTagName("pizzaName");
xmlNode[0].InnerText = "Pizza Name";
xmlNode = xmlDoc.GetElementsByTagName("pizzaDescription");
xmlNode[0].InnerText = "Pizza Description";
//write the custom xml data into the customxmlpart
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(customPart.GetStream(System.IO.FileMode.Create), System.Text.Encoding.UTF8);
writer.WriteRaw(xmlDoc.InnerXml);
writer.Flush();
writer.Close();
fileStream.Close();
br.Close();
mainPart.Document.Save();
wordDoc.Close();
and the XML template of the project would something like:
<?xml version="1.0"?>
<Pizza xmlns="http://Test/ContentControls/Pizzas">
<pizzaPic></pizzaPic>
<pizzaName></pizzaName>
<pizzaDescription></pizzaDescription>
</Pizza>
I hope this helps.
Calvin Gao[MSFT]
MSDN Community Support | Feedback to us
Maybe you are looking for
-
My audio and video are in sync, but get out of sync as it goes along?
I recorded an audio file on Garageband and I am trying to line it up with a iMovie video. At first, the audio and video sync up perfectly, but as the video goes along, a delay starts to appear and grows bigger and bigger. Anybody has a solution?
-
Flex sdk 4.6 maven repo
Hi there wasa blog post last October (http://justinjmoses.wordpress.com/2011/10/) saying that Adobe was getting invovled in getting the flex 4.6 sdk into a maven repository. There is still no mention of this either here or in the flexmojos google gro
-
Hi - For the life of me I cannot figure out how to take a beat I have created and then loop it. There is no "create loop" option as there was in previous versions. Help please. Thanks
-
DropDownList connected to the database table
Hello Gurus. How it is possible to create a value help for a field in web dynpro application and connect it to the database table column. In ABAP it seems simple to me, but what about web dynpro application written in Java? I use the Sap Netweaver De
-
VIBE UI - Bug in Selfie and Front Camera Mode
I am now testing and reviewing Lenov Vibe Z2. This model has 8MP selfie camera, so you would assume that it should work well with a selfie stick. But it does not. A selfie stick that works reasonably well with any other phone, will just increase the