FYI - Copying CSV data to the clipboard for Excel

new topic     » topic index » view thread      » older message » newer message

All,

For those who ever wanted to copy the contents of a list view to the 
clipboard to be opened by Excel, I submit the following procedure:

procedure CopyToClipPI_onClick(integer self, integer event, sequence
params)--params is ()
	sequence lv_select, lv_data, wrk_text, columns
	atom rtn_code, wrk_text_p, csv_fmt_p, clip_format
	
	-- Get the CSV clipboard format
	csv_fmt_p = allocate_string("Csv")
	clip_format = w32Func(xRegisterClipboardFormat,{csv_fmt_p})
	free(csv_fmt_p)
	-- Open the clipboard
	rtn_code = w32Func(xOpenClipboard, {getHandle(Main)})
	if rtn_code then
		-- Clean out the clipboard
		rtn_code = w32Func(xEmptyClipboard,{})
		-- First load the column names
		wrk_text = {}
		columns = getColumnHeadings(ResultsLV)
		for i = 1 to length(columns) do
			wrk_text &= columns[i][2]
			if i < length(columns) then
				wrk_text &= ","
			else
				wrk_text &= "\n"
			end if
		end for
		-- Now get the selected rows
		lv_select = getLVSelected(ResultsLV)
		for i = 1 to length(lv_select) do
			-- Get the data for the selected row
			lv_data = getLVAllText(ResultsLV, lv_select[i])
			-- Add on the data columns
			for x = 1 to length(lv_data) do
				wrk_text &= lv_data[x]
				if x < length(lv_data) then
					wrk_text &= ","
				else
					wrk_text &= "\n"
				end if
			end for
		end for
		-- Append a null terminator
		wrk_text &= #00
		-- Put to the clipboard
		wrk_text_p = allocate_string(wrk_text)
		rtn_code = w32Func(xSetClipboardData,{clip_format,wrk_text_p})
		-- Close the clipboard
		w32Proc(xCloseClipboard,{})
		-- free the memory
		free(wrk_text_p)
	end if
end procedure	


At first I tried CF_TEXT as the format for the call to SetClipboardData
but it'd didn't parse the fields out in Excel.  After searching the new
for some examples I found the call to RegisterClipboardFormat is needed
in this case to get the format value to pass to SetClipboardData.

Jonas Temple
http://www.yhti.net/~jktemple

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu