Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to convert a list to excel and return that to the browser. I have finished the process. But the issue is that there is a column in the excel that shrink by default. I have a credit card number column, i have taken that as a string, still that shrinks. (16 digit in the column).

Kindly let me know how can i solve this one.

What I have tried:

C#
var reducedList = FinReportClassList.Select(e => new { e.ConfNo, e.FIleNo, e.Guest, e.CardNo, e.VCAStatus, e.Txn, e.Post, e.Supplier, e.State, e.Country, e.OrgAmount, e.orgcuuactual, e.BillAmount, e.BillCurrency, e.UserName }).ToList();
                         Response.ClearContent();
                         Response.AddHeader("content-disposition", "attachment;filename=VCA-Transactions.xls");
                         Response.AddHeader("Content-Type", "application/vnd.ms-excel");
                         WriteTsv(reducedList, Response.Output);
                         Response.End();


         public void WriteTsv<t>(IEnumerable<t> data, TextWriter output)
         {
             PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));


             foreach (PropertyDescriptor prop in props)
             {                 
                 if(prop.DisplayName=="ConfNo")
                 {
                     output.Write("Confirmation No"); // header
                     output.Write("\t");  
                 }
                 if (prop.DisplayName == "FIleNo")
                 {
                     output.Write("FIle No"); // header
                     output.Write("\t");  
                 }
                 if (prop.DisplayName == "Guest")
                 {
                     output.Write("Guest Name"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "CardNo")
                 {
                     output.Write("Card No"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "VCAStatus")
                 {
                     output.Write("Card Status"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "Txn")
                 {
                     output.Write("Txn Date"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "Post")
                 {
                     output.Write("Post Date"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "Supplier")
                 {
                     output.Write("Supplier Name"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "State")
                 {
                     output.Write("Supplier State"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "Country")
                 {
                     output.Write("Supplier Country"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "OrgAmount")
                 {
                     output.Write("Swipped Amount"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "orgcuuactual")
                 {
                     output.Write("Swipped Currency"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "BillAmount")
                 {
                     output.Write("Bill Amount"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "BillCurrency")
                 {
                     output.Write("Bill Currency"); // header
                     output.Write("\t");
                 }
                 if (prop.DisplayName == "UserName")
                 {
                     output.Write("Agent"); // header
                     output.Write("\t");
                 }  
             }
             output.WriteLine();
             foreach (T item in data)
             {
                 foreach (PropertyDescriptor prop in props)
                 {
                     output.Write(prop.Converter.ConvertToString(
                          prop.GetValue(item)));
                     output.Write("\t");
                 }
                 output.WriteLine();
             }
         }
Posted
Updated 28-Mar-16 3:46am
v2

1 solution

your code seems to write the file in CSV format which is not excel.
So you can't control column width and have default width, not a shrink.

In order to control excel things like formatting, you must use an excel format like xlsx or xml. Find the library that will fit your needs.
 
Share this answer
 

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