Load an Existing Excel File and Programmatically Change Data with LEADTOOLS

With the new LEADTOOLS Excel SDK, developers can programmatically create or edit Excel files. This blog is the first in a three-part series exploring the toolkit’s basic yet powerful features. In this first part, we’ll demonstrate how to modify a small table within a reimbursement form for an employer.

Here is the current table we are working with, which tracks the cities and states driven through, the car brands used, the distance traveled in kilometers, and the cost of the trip. In this example, we will demonstrate how easy it is to convert the distances from kilometers to miles using LEADTOOLS. Let’s switch over to our code to show how straightforward this task is.

Before we begin – be sure to grab the latest LEADTOOLS SDK to gain access to all the new features with V23! With 60 free days of evaluation, you can follow along with this blog and explore LEADTOOLS’ advanced SDKs for intelligent document processing, OCR/ICR, image processing, barcode, and more.


using Leadtools;
using Leadtools.Document.LEADOffice.Sheet;
using System.Text;

namespace ProgrammaticallyExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            InitLEAD();
            //we load in our exsiting Excel file's workbook
            LEADWorkbook workbook = LEADWorkbookFactory.LoadFromFile("C:\\temp\\CityTravelExpenses.xlsx", null);

            try
            {
                if (workbook != null)
                {
                    ConvertToMiles(workbook, 3);
                    SaveWorkbook(workbook);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

The InitLEAD method sets our license files. For more information on this, please check out this tutorial that will go more in depth. We will be focusing on ConvertToMiles and SaveWorkbook methods.


static void ConvertToMiles(LEADWorkbook workbook, int column)
{
    //since we are only working in on the first sheet, we grab that one only
    Sheet sheet = workbook.GetSheetAt(0);

    for (int i = 1; i < sheet.LastRowIndex + 1; i++)
    {
        Row row = sheet.GetRowAt(i);
        //since we know the distance column is the only place we want to edit cell data, we only look in that column for each row we loop through
        var cell = row.GetCellAt(column);
        if (cell != null)
        {
            //we take the existing data and multiply it by a set value to convert from kilometers to miles
            cell.SetCellValue(Math.Round(cell.NumericCellValue * 0.621371));
        }
    }
}

This function takes in two parameters: the workbook declared in the main method and the specified column to be edited. Since our Excel file contains only one sheet, we ensure that it is the active sheet. Then, we create a for loop to cycle through all the rows in the sheet, starting from row 1 to avoid altering the header row. For each row, we specify the column to be edited, giving us the coordinates to locate the cell (e.g., for the first data row and the third column, the coordinates would be [1, 3]). Finally, we convert the distance from kilometers to miles using basic math, round the result, and set the cell value to the computed number.


static void SaveWorkbook(LEADWorkbook workbook)
{

    string filePath = @"C:\\temp\\CityTravelExpenses_fixedDistance.xlsx";
    using FileStream fs = new(filePath, FileMode.CreateNew);
    workbook.Save(fs);
}

Saving a workbook is easily done with just 3 lines of code:

  1. Create a string to house where we will save the file
  2. Use a FileStream to create a new file at a specified location
  3. Save the workbook data to the newly created file

In these results, you can see that the distance column has been successfully changed from kilometers to miles.

That's it! This simple example demonstrates how you can use the LEADTOOLS Excel SDK to programmatically alter and save Excel files. With our FREE 60 day evaluation SDK, fully supported by our expert support team, this is simply too good to pass up. Try integrating the LEADTOOLS SDK into your current or next application!

Here is the project file for the code we created in this blog. Stay tuned for part two of this three-part blog series showcasing the LEADTOOLS Excel SDK!

Posted in Document Imaging, Excel | Tagged | Leave a comment

Non-Standard Paper Sizes for Printing Physical and Virtually? Not a Problem with LEADTOOLS!

LEADTOOLS Support recently had a case where a customer was using an old AS/400 machine, a dot matrix printer that is often used with COBOL or Fortran to print results. The issue they faced was that while normal standard letter paper is 8.5” x 11”, this machine printed paper at 15” x 12”.

Continue reading
Posted in Document Imaging, Virtual Printer | Tagged | Leave a comment

See Us In Seattle at Microsoft Build 2024!

Microsoft Build is just around the corner – May 21st-23rd and LEADTOOLS is a Featured Partner once again! Join us, peers, and Microsoft leaders to explore the latest innovations in code and application development. Build is THE premiere developer conference with topics focused on AI Development, Cloud Platform, Copilot, Data & Analytics, Developer Tools, Low-code, Security, and Windows. Catch LEADTOOLS both digitally and in Seattle again this year and read on to learn more about our sessions, giveaways, and more!

Continue reading
Posted in General | Leave a comment

Capture it all with LEADTOOLS Multi-Capture Video Support

Being able to capture audio and video from multiple sources is useful for a range of industries, from conducting online meetings, webinars, or conferences to creating content online or instructional videos to enhance the learning experience. The new LEADTOOLS Multi-Capture Video Support gives developers the power to build powerful video streaming applications with ease. This technology is new with V23 and not only does it do the work for you, but it allows you to use a wide variety of supported input sources to capture all the angles you need!

Continue reading
Posted in Multimedia Imaging | Tagged | Leave a comment

LEADTOOLS New Multimedia Speech Recognition Demo

With LEADTOOLS V23, comes new demos! Our new Multimedia Speech Recognition Demo is able to take in any of our supported video file formats and transcribe a script based on the words that are being spoken. This blog will go over some of the features in this powerful demo application.

Continue reading
Posted in Multimedia Imaging, Speech Recognition | Tagged | Leave a comment