0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

.NET:ClosedXMLを利用し、Excelファイルを読み取り専用でデータをインポート

Posted at
using ClosedXML.Excel;

namespace Service
{
    internal class ImportExcel
    {
        public void ImportMyExcel(string filename)
        {
            try
            {
                // 読み込み専用で開く
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XLWorkbook workbook = new XLWorkbook(fs);
                IXLWorksheet worksheet = workbook.Worksheet(1);
                IXLCell cell;

                int i = 1;
                while (true)
                {
                    cell = worksheet.Cell(i, 1);
                    if (String.IsNullOrEmpty(cell.Value.ToString())) break;
                    // TODO:業務ロジック
                    i++;
                }
            }
            catch
            {
                throw (new AppBaseException("Error:Read excel file. : " + filename));
            }
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?