Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am able to create .xls file successfully using stream writer and string builder and I am able to save it physically on the disk. Now my boss asked me to add a watermark in the background. How can I do that ?????
here is my code:
<pre lang="c#">
public static void ExportToFile(string filename, string filepath, System.Data.DataTable dtResult, string webServerFileLink)
    {
        string sFlag = "Error";

        StreamWriter sw = new StreamWriter(filepath, false);
        StringBuilder dataToExport = new StringBuilder();
        sw.WriteLine(Convert.ToString(dataToExport));
        
        try
        {
            if (filename.Trim() != "" && filepath != "" && dtResult.Rows.Count != 0)
            {
                 dataToExport.Append("<table width=\"100%\">");
                dataToExport.Append("<tr>");
               
                foreach (DataColumn dCol in dtResult.Columns)
                {
                    dataToExport.Append("<td style=\"border:#ccc 1px dotted; height:40px; vertical-align:middle;text-align:center;min-width:100px; background-color:#D0EFF5;\">");
                    dataToExport.Append(HttpContext.Current.Server.HtmlEncode(dCol.ColumnName));
                    dataToExport.Append("</td>");
                    
                }
             

                dataToExport.Append("</tr>");

                foreach (DataRow dRow in dtResult.Rows)
                {
                    dataToExport.Append("<tr>");
                    foreach (object obj in dRow.ItemArray)
                    {
                        dataToExport.Append("<td style=\"border:#ccc 1px dotted;min-width:100px; vertical-align:middle;text-align:center;\">");
                        dataToExport.Append(HttpContext.Current.Server.HtmlEncode(obj.ToString()));
                        dataToExport.Append("</td>");
                      
                    }
                   
                    dataToExport.Append("</tr>");
                }

                dataToExport.Append("</table>");
                
            }
            sw.WriteLine(Convert.ToString(dataToExport));
            sFlag = "Success";

            sw.Close();
           

        }



        catch (Exception ex)
        {
            throw ex;
        }


       
      
    }  



Please Help !
Posted

1 solution

An xls is an Excel spreadsheet. I am not sure it supports watermarks, they tend to exist on bitmaps. The code you posted creates a table, which has nothing to do with Excel, unless you have code you didn't show that turns tables into an xls file. If you do have such a library, then it may have methods to do what you want, although I doubt it, as it does not seem to work with Excel directly. Hard to say from the code you posted. The Micrsoft Tools for Office let you interact with Office files in .NET. If watermarks exist at all, then perhaps that's where you can do it.

But given that an Excel file is not a bitmap, anything you add, an end use can remove.
 
Share this answer
 
Comments
Taresh Uppal 25-Aug-12 5:35am    
here is the rest of code:

string strDwnload_file_name = dwnlod_file_name(strWorkOrder);
ExportToFile(strDwnload_file_name, HttpContext.Current.Server.MapPath("~//download//" + strDwnload_file_name + ".xls"), dtResult, "http://www.cybex.in/ODMS//download" + "/" + strDwnload_file_name + ".xls");
//CreateCSVFile(dtResult, HttpContext.Current.Server.MapPath("~//download//" + strDwnload_file_name + ".csv"), "http://www.cybex.in/ODMS//download" + "/" + strDwnload_file_name + ".csv");
string Url_of_HomePage = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath + "/";
//download_url = "" + Url_of_HomePage + "download/" + strDwnload_file_name + ".csv";
download_url = "" + Url_of_HomePage + "download/" + strDwnload_file_name + ".xls";

I do nothing but pick a unique name for downloading the file, convert it into url, add .xls and thats all.
Christian Graus 25-Aug-12 5:37am    
There is no database code here, anywhere
Taresh Uppal 25-Aug-12 5:42am    
string download_url = "";
int iResult = 0;
string strMonthName = Select_Month_Name(From);
string str_Search_Query_For_Data = search(strData, strHSCODE, strComb, strProd,strProdNot, From, To, strMonthName);

System.Data.DataTable dtResult = DAO_Con_diff_db.Select_datatable(conn_str_2012, str_Search_Query_For_Data);

if (dtResult.Rows.Count != 0)
{
iResult = 1;

if (strData == "EXPORT")
{
dtResult.Columns[5].ColumnName = "FOB VALUE INR";
}
else
{
dtResult.Columns[5].ColumnName = "ASSESSABLE VALUE INR";
}

string strDwnload_file_name = dwnlod_file_name(strWorkOrder);
ExportToFile(strDwnload_file_name, HttpContext.Current.Server.MapPath("~//download//" + strDwnload_file_name + ".xls"), dtResult, "http://www.cybex.in/ODMS//download" + "/" + strDwnload_file_name + ".xls");

string Url_of_HomePage = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath + "/";

download_url = "" + Url_of_HomePage + "download/" + strDwnload_file_name + ".xls";

the data is fetched inside a datatable and than this datatable is modified in my menthods i showed u above to make it as a xls extension.
Taresh Uppal 25-Aug-12 5:43am    
we make a search query for minning data as simple sql query which brings the data and than I take action accordingly...
Christian Graus 25-Aug-12 5:45am    
So you don't STORE the data, you FETCH it. All of this has me lost. Where does your xls file come from ? What is creating it ? The code you posted writes a HTML table, not an xls file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900