Change the Excel column format from number to Text in JavaHow to round a number to n decimal places in JavaHow do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?How to append text to an existing file in JavaReading a plain text file in JavaChange date format in a Java stringRemoving whitespace from strings in JavaHow to read a large text file line by line using Java?

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

How can I shoot a bow using strength instead of dexterity?

"Mouth-breathing" as slang for stupidity

What is the most difficult concept to grasp in Calculus 1?

How to gracefully leave a company you helped start?

Are there really no countries that protect Freedom of Speech as the United States does?

graphs in latex

Prestidigitation to replace bathing and washing clothes worn?

What is the farthest a camera can see?

Doesn't the speed of light limit imply the same electron can be annihilated twice?

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

Bringing Power Supplies on Plane?

How far did Gandalf and the Balrog drop from the bridge in Moria?

Cycle of actions and voice signals on a multipitch climb

K-Type Thermocouple, Instrumentation Op-Amp and Arduino

Does an Irish VISA WARNING count as "refused entry at the border of any country other than the UK?"

Should I leave building the database for the end?

Why not demand President's/candidate's financial records instead of tax returns?

Help, I cannot decide when to start the story

Why aren’t there water shutoff valves for each room?

Are there examples in Tanach of 3 or more parties having an ongoing conversation?

Do beef farmed pastures net remove carbon emissions?

Why is there a large performance impact when looping over an array with 240 or more elements?

Shifting tenses in the middle of narration



Change the Excel column format from number to Text in Java


How to round a number to n decimal places in JavaHow do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?How to append text to an existing file in JavaReading a plain text file in JavaChange date format in a Java stringRemoving whitespace from strings in JavaHow to read a large text file line by line using Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm trying to create an Excel template with all the columns / cells as text.
The Excel sheet has 5 columns



Here is my code:



@RequestMapping(value = "/app/downloadTemplateForAssociateBankingDetails", method = RequestMethod.GET)
public void downloadTemplateForAssociateBankingDetails(HttpServletRequest request,HttpServletResponse response) throws Exception
@SuppressWarnings("resource")
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("AssociateBankingTemplate");
String[] tableColumns = Constants.DATA_MIGRATION_ASSOCIATE_BANKING_EXCEL_HEADERS;
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();

Row row = sheet.createRow(0);
int cellnum = 0;
for (String key : tableColumns)

Cell cell = row.createCell(cellnum++);
cellStyle.setDataFormat(
fmt.getFormat("@"));
cell.setCellStyle(cellStyle);
cell.setCellValue((String)key);



try
File file = new File("Associate_Banking_Template.xls");
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);

out.close();
out.flush();

String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";

response.setContentType(mimeType);
StringBuilder fileNameSB = new StringBuilder();
response.setHeader("Content-Disposition", fileNameSB.append("attachment; filename="").append(file.getName()).append(""").toString());
response.setContentLength((int) file.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().close();
response.getOutputStream().flush();

catch (Exception e)
e.printStackTrace();












share|improve this question


























  • Everytime getting cell format as Number not text

    – Rajat Rana
    Mar 27 at 11:25

















0















I'm trying to create an Excel template with all the columns / cells as text.
The Excel sheet has 5 columns



Here is my code:



@RequestMapping(value = "/app/downloadTemplateForAssociateBankingDetails", method = RequestMethod.GET)
public void downloadTemplateForAssociateBankingDetails(HttpServletRequest request,HttpServletResponse response) throws Exception
@SuppressWarnings("resource")
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("AssociateBankingTemplate");
String[] tableColumns = Constants.DATA_MIGRATION_ASSOCIATE_BANKING_EXCEL_HEADERS;
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();

Row row = sheet.createRow(0);
int cellnum = 0;
for (String key : tableColumns)

Cell cell = row.createCell(cellnum++);
cellStyle.setDataFormat(
fmt.getFormat("@"));
cell.setCellStyle(cellStyle);
cell.setCellValue((String)key);



try
File file = new File("Associate_Banking_Template.xls");
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);

out.close();
out.flush();

String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";

response.setContentType(mimeType);
StringBuilder fileNameSB = new StringBuilder();
response.setHeader("Content-Disposition", fileNameSB.append("attachment; filename="").append(file.getName()).append(""").toString());
response.setContentLength((int) file.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().close();
response.getOutputStream().flush();

catch (Exception e)
e.printStackTrace();












share|improve this question


























  • Everytime getting cell format as Number not text

    – Rajat Rana
    Mar 27 at 11:25













0












0








0








I'm trying to create an Excel template with all the columns / cells as text.
The Excel sheet has 5 columns



Here is my code:



@RequestMapping(value = "/app/downloadTemplateForAssociateBankingDetails", method = RequestMethod.GET)
public void downloadTemplateForAssociateBankingDetails(HttpServletRequest request,HttpServletResponse response) throws Exception
@SuppressWarnings("resource")
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("AssociateBankingTemplate");
String[] tableColumns = Constants.DATA_MIGRATION_ASSOCIATE_BANKING_EXCEL_HEADERS;
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();

Row row = sheet.createRow(0);
int cellnum = 0;
for (String key : tableColumns)

Cell cell = row.createCell(cellnum++);
cellStyle.setDataFormat(
fmt.getFormat("@"));
cell.setCellStyle(cellStyle);
cell.setCellValue((String)key);



try
File file = new File("Associate_Banking_Template.xls");
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);

out.close();
out.flush();

String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";

response.setContentType(mimeType);
StringBuilder fileNameSB = new StringBuilder();
response.setHeader("Content-Disposition", fileNameSB.append("attachment; filename="").append(file.getName()).append(""").toString());
response.setContentLength((int) file.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().close();
response.getOutputStream().flush();

catch (Exception e)
e.printStackTrace();












share|improve this question
















I'm trying to create an Excel template with all the columns / cells as text.
The Excel sheet has 5 columns



Here is my code:



@RequestMapping(value = "/app/downloadTemplateForAssociateBankingDetails", method = RequestMethod.GET)
public void downloadTemplateForAssociateBankingDetails(HttpServletRequest request,HttpServletResponse response) throws Exception
@SuppressWarnings("resource")
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("AssociateBankingTemplate");
String[] tableColumns = Constants.DATA_MIGRATION_ASSOCIATE_BANKING_EXCEL_HEADERS;
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();

Row row = sheet.createRow(0);
int cellnum = 0;
for (String key : tableColumns)

Cell cell = row.createCell(cellnum++);
cellStyle.setDataFormat(
fmt.getFormat("@"));
cell.setCellStyle(cellStyle);
cell.setCellValue((String)key);



try
File file = new File("Associate_Banking_Template.xls");
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);

out.close();
out.flush();

String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";

response.setContentType(mimeType);
StringBuilder fileNameSB = new StringBuilder();
response.setHeader("Content-Disposition", fileNameSB.append("attachment; filename="").append(file.getName()).append(""").toString());
response.setContentLength((int) file.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().close();
response.getOutputStream().flush();

catch (Exception e)
e.printStackTrace();









java spring-mvc apache-poi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 12:00









Richard Neish

5,5263 gold badges28 silver badges59 bronze badges




5,5263 gold badges28 silver badges59 bronze badges










asked Mar 27 at 11:22









Rajat RanaRajat Rana

31 bronze badge




31 bronze badge















  • Everytime getting cell format as Number not text

    – Rajat Rana
    Mar 27 at 11:25

















  • Everytime getting cell format as Number not text

    – Rajat Rana
    Mar 27 at 11:25
















Everytime getting cell format as Number not text

– Rajat Rana
Mar 27 at 11:25





Everytime getting cell format as Number not text

– Rajat Rana
Mar 27 at 11:25












2 Answers
2






active

oldest

votes


















1














Who is telling you that the cell isn't in text format? MS Excel? LibreOffice Calc?



The following code works for me and should give you a cell in text format.



public class TextFormat 

public static void main(String[] args) throws Exception

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Text");

CellStyle cellStyle = workbook.createCellStyle();
DataFormat fmt = workbook.createDataFormat();
cellStyle.setDataFormat(fmt.getFormat("@"));
cell.setCellStyle(cellStyle);

OutputStream fileOut = new FileOutputStream("workbook.xls");
workbook.write(fileOut);
workbook.close();








share|improve this answer



























  • LibreOffice Calc

    – Rajat Rana
    Mar 29 at 6:12











  • When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

    – Martin
    Mar 29 at 6:43



















0














Something like:



Worksheets("Sheet1").Range("A17").NumberFormat = "@"


I am not sure but I think that "@" means Text format.
That is only for one cell, if you want all cells I think it could be something like this:



Worksheets("Sheet1").Range("B:B").NumberFormat="@"


in case you want only column B



`Worksheets("Sheet1").Range("A:E").NumberFormat="@"` 


if you want all your data is read like a text.






share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376007%2fchange-the-excel-column-format-from-number-to-text-in-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Who is telling you that the cell isn't in text format? MS Excel? LibreOffice Calc?



    The following code works for me and should give you a cell in text format.



    public class TextFormat 

    public static void main(String[] args) throws Exception

    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Text");

    CellStyle cellStyle = workbook.createCellStyle();
    DataFormat fmt = workbook.createDataFormat();
    cellStyle.setDataFormat(fmt.getFormat("@"));
    cell.setCellStyle(cellStyle);

    OutputStream fileOut = new FileOutputStream("workbook.xls");
    workbook.write(fileOut);
    workbook.close();








    share|improve this answer



























    • LibreOffice Calc

      – Rajat Rana
      Mar 29 at 6:12











    • When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

      – Martin
      Mar 29 at 6:43
















    1














    Who is telling you that the cell isn't in text format? MS Excel? LibreOffice Calc?



    The following code works for me and should give you a cell in text format.



    public class TextFormat 

    public static void main(String[] args) throws Exception

    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Text");

    CellStyle cellStyle = workbook.createCellStyle();
    DataFormat fmt = workbook.createDataFormat();
    cellStyle.setDataFormat(fmt.getFormat("@"));
    cell.setCellStyle(cellStyle);

    OutputStream fileOut = new FileOutputStream("workbook.xls");
    workbook.write(fileOut);
    workbook.close();








    share|improve this answer



























    • LibreOffice Calc

      – Rajat Rana
      Mar 29 at 6:12











    • When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

      – Martin
      Mar 29 at 6:43














    1












    1








    1







    Who is telling you that the cell isn't in text format? MS Excel? LibreOffice Calc?



    The following code works for me and should give you a cell in text format.



    public class TextFormat 

    public static void main(String[] args) throws Exception

    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Text");

    CellStyle cellStyle = workbook.createCellStyle();
    DataFormat fmt = workbook.createDataFormat();
    cellStyle.setDataFormat(fmt.getFormat("@"));
    cell.setCellStyle(cellStyle);

    OutputStream fileOut = new FileOutputStream("workbook.xls");
    workbook.write(fileOut);
    workbook.close();








    share|improve this answer















    Who is telling you that the cell isn't in text format? MS Excel? LibreOffice Calc?



    The following code works for me and should give you a cell in text format.



    public class TextFormat 

    public static void main(String[] args) throws Exception

    Workbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Text");

    CellStyle cellStyle = workbook.createCellStyle();
    DataFormat fmt = workbook.createDataFormat();
    cellStyle.setDataFormat(fmt.getFormat("@"));
    cell.setCellStyle(cellStyle);

    OutputStream fileOut = new FileOutputStream("workbook.xls");
    workbook.write(fileOut);
    workbook.close();









    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 27 at 16:18

























    answered Mar 27 at 16:00









    MartinMartin

    1039 bronze badges




    1039 bronze badges















    • LibreOffice Calc

      – Rajat Rana
      Mar 29 at 6:12











    • When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

      – Martin
      Mar 29 at 6:43


















    • LibreOffice Calc

      – Rajat Rana
      Mar 29 at 6:12











    • When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

      – Martin
      Mar 29 at 6:43

















    LibreOffice Calc

    – Rajat Rana
    Mar 29 at 6:12





    LibreOffice Calc

    – Rajat Rana
    Mar 29 at 6:12













    When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

    – Martin
    Mar 29 at 6:43






    When you try my code, open workbook.xls, right click on the first cell and select “Format Cells...” then you should see the format code is set to @.

    – Martin
    Mar 29 at 6:43














    0














    Something like:



    Worksheets("Sheet1").Range("A17").NumberFormat = "@"


    I am not sure but I think that "@" means Text format.
    That is only for one cell, if you want all cells I think it could be something like this:



    Worksheets("Sheet1").Range("B:B").NumberFormat="@"


    in case you want only column B



    `Worksheets("Sheet1").Range("A:E").NumberFormat="@"` 


    if you want all your data is read like a text.






    share|improve this answer





























      0














      Something like:



      Worksheets("Sheet1").Range("A17").NumberFormat = "@"


      I am not sure but I think that "@" means Text format.
      That is only for one cell, if you want all cells I think it could be something like this:



      Worksheets("Sheet1").Range("B:B").NumberFormat="@"


      in case you want only column B



      `Worksheets("Sheet1").Range("A:E").NumberFormat="@"` 


      if you want all your data is read like a text.






      share|improve this answer



























        0












        0








        0







        Something like:



        Worksheets("Sheet1").Range("A17").NumberFormat = "@"


        I am not sure but I think that "@" means Text format.
        That is only for one cell, if you want all cells I think it could be something like this:



        Worksheets("Sheet1").Range("B:B").NumberFormat="@"


        in case you want only column B



        `Worksheets("Sheet1").Range("A:E").NumberFormat="@"` 


        if you want all your data is read like a text.






        share|improve this answer













        Something like:



        Worksheets("Sheet1").Range("A17").NumberFormat = "@"


        I am not sure but I think that "@" means Text format.
        That is only for one cell, if you want all cells I think it could be something like this:



        Worksheets("Sheet1").Range("B:B").NumberFormat="@"


        in case you want only column B



        `Worksheets("Sheet1").Range("A:E").NumberFormat="@"` 


        if you want all your data is read like a text.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 11:53









        AdriánAdrián

        14 bronze badges




        14 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376007%2fchange-the-excel-column-format-from-number-to-text-in-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript