How to write Excel files in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.ย. 2024
  • Today we are looking at how to write to an excel file! We quickly open an existing excel file and modify it. After that, I show how to save that same excel file to a new file!
    If you need to write one value, you can easily do that, we also cover how to store your values in an array and write that array to the file!
    I recommend using this master template style Opening , editing, saving design pattern because it allows you to design a complicated excel file and then add data to it.
    At any rate, I hope you enjoy this deep dive into writing to Excel files using C#!
    tags: how to,c# winforms,c# tutorial,c# excel tutorial - #1 - open and read excel files,C# Write excel file,c# open excel file,write excel,csharp,tim corey,iamtimcorey,Fox Learn,C# write excel,*.xls,*.xlsm,programmingknowledge,microsoft excel,c sharp (programming language)
    #excel #Csharp #programming

ความคิดเห็น • 29

  • @DarrenG
    @DarrenG  2 ปีที่แล้ว +2

    Thanks for watching! If you have any questions just let me know!!

    • @dhairyakhanna277
      @dhairyakhanna277 ปีที่แล้ว

      just tell me where cellrange.set_Value method is coming ??

    • @johncenator3146
      @johncenator3146 2 หลายเดือนก่อน

      I'm late but if you want to write to the file rather than just create a new one couldn't you just programmatically set that file to have read - write permissions with a powershell script?

  • @mohammedghafri4258
    @mohammedghafri4258 2 ปีที่แล้ว +3

    That was a great tutorial! but I have a little note; set_Value attribute does not exist in my visual studio 2022 and I think Value2 attribute is the alternative for that.
    Thank you man!

  • @troylambert3190
    @troylambert3190 ปีที่แล้ว

    I never thought Nick Kroll with a different haircut would ever help me with Excel interoperability but here we are! (Good video in all seriousness)

  • @newmarek3
    @newmarek3 2 ปีที่แล้ว +3

    To help someone of you save time. If you want it to create a file programmatically. Instead of wb=excel.Workbooks.open(); you want wb=excel.Workbooks.add(); and if you dont want it to ask you to save before you close you want excel.DisplayAlerts = False; then you need: object missingValue = System.Reflection.Missing.Value; because your close statement is wb.Close(false, missingValue, missingValue);

  • @aritrapaul1418
    @aritrapaul1418 4 หลายเดือนก่อน

    Thanks bro

  • @GmanGavin1
    @GmanGavin1 2 ปีที่แล้ว

    This is such a great tutorial.

  • @ThaoMeoTV
    @ThaoMeoTV ปีที่แล้ว

    Thanks sir!

  • @Dandanukadan
    @Dandanukadan ปีที่แล้ว +1

    Thanks Darren, a few quick feedback and requests as follows
    1]Your tutorials are awesome but if you can slow it down a little bit it helps newbies like me to understand fully what you are trying to explain.
    2)Would you helps us with the following request - I want to enter value dynamically in a text box during run time and that needs to be captured in a parameter to be parsed onto Excel.
    Thank you.

    • @DarrenG
      @DarrenG  ปีที่แล้ว +1

      I'm sorry that I go to fast, I think the real issue comes down to how I edited the video for TH-cam. I cut out the silence and it just creates a very fast paced video.

  • @humanaccount2754
    @humanaccount2754 ปีที่แล้ว

    thank you

  • @vorapobautomation9760
    @vorapobautomation9760 หลายเดือนก่อน

    How to load Data From Notepad (.CSV) to DataGridView in C# Windows Form Application ?

  • @frevanneste7595
    @frevanneste7595 ปีที่แล้ว +1

    hi, If you make a template in excel and write into that template, will this work once you publish the app, if the user doesn't have the template locally on his computer (he only has the published app)?

    • @DarrenG
      @DarrenG  ปีที่แล้ว +1

      If I'm understanding you correctly, the user would definitely need the template.... You are effectively copying the template and editing it while you copy it.
      If the file isn't in place to be copied, that's an error.

  • @kanadalux3621
    @kanadalux3621 ปีที่แล้ว

    Amigo, una consulta por favor, quiero escribir formulas anidadas en una celda usando C# pero esas fórmulas se generan concatenando variable. El error que presenta es que se inserta la fórmula pero con un @ antes de "=". Ya probé usar diferentes librerias como EPPLUS, NPOI , y sigo con ese error .

  • @ShashankNaik-ml2mh
    @ShashankNaik-ml2mh 5 หลายเดือนก่อน +1

    WriteExcel function is not showing.its showing error

  • @OranMcGread
    @OranMcGread 2 ปีที่แล้ว

    Thanks for the video, how would you add data to the next empty column in the spreadsheet instead of specifying the range?

    • @DarrenG
      @DarrenG  2 ปีที่แล้ว +2

      To my knowledge, You do have to specify the range
      You can use worksheet.UsedRange() to get the currently used range, which would then allow you to figure out where the next unused cell is located to declare your range to add the data.
      Hopefully that makes sense
      docs.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.worksheet.usedrange?view=vsto-2017

    • @OranMcGread
      @OranMcGread 2 ปีที่แล้ว

      @@DarrenG thanks I’ll look into that

  • @codymckinney4934
    @codymckinney4934 7 หลายเดือนก่อน

    "cannot use object linking or embedding" error when opening the excel file and running code

  • @jeremiahsease5784
    @jeremiahsease5784 ปีที่แล้ว

    Okay So how you write to excel using blazor?

  • @lemonlearner7315
    @lemonlearner7315 2 ปีที่แล้ว +1

    hey!! nice video! how can i delete an entire row?

    • @lemonlearner7315
      @lemonlearner7315 2 ปีที่แล้ว +1

      hey, you can delete an entire row with the next code:
      cell.EntireRow.Delete(Type.Missing); //cell contains the range

  • @fabianralph5472
    @fabianralph5472 ปีที่แล้ว +2

    How to fix read only ?

    • @johncenator3146
      @johncenator3146 2 หลายเดือนก่อน

      Use a bash or powershell script or manually edit the permissions on the file.

  • @kxrxbo
    @kxrxbo 6 หลายเดือนก่อน

    Where are you rushing to ?😂

  • @igorthelight
    @igorthelight ปีที่แล้ว +1

    A working and commented code (it's a Console application tho):
    using Microsoft.Office.Interop.Excel;
    using System;
    namespace ExcelParser
    {
    internal class Program
    {
    static void Main(string[] args)
    {
    WriteExcel(@"C:\sheet.xlsx");
    Console.ReadKey();
    }
    static void WriteExcel(string filePath)
    {
    Application excel = new Application();
    Workbook wb = excel.Workbooks.Open(filePath);
    Worksheet ws = wb.Worksheets[1];
    // Reading and wrtiting to one cell
    //Range cell = ws.Cells[1, 3]; // Less human friendly way
    Range cell = ws.Range["C1"];
    cell.Value = "Pizza";
    // Writing to a whole row or column
    Range cells = ws.Range["C1:F1"];
    string[] things = { "WOW!", "Hamburger", "Cars", "Trees" };
    cells.set_Value(XlRangeValueDataType.xlRangeValueDefault, things);
    wb.Save();
    //wb.SaveAs(filePath); // If you want to save it with another name or in another folder
    wb.Close();
    }
    }
    }

  • @GreyHatGenX
    @GreyHatGenX ปีที่แล้ว

    commnet