Converti NumPy in Excel

Converti l’array NumPy in Excel

Ecco un frammento di codice di esempio per dimostrare come importare dati da un array NumPy a un file Excel utilizzando Aspose.Cells for Python via .NET:

  1. Crea un esempio di dati dell’array NumPy.
  2. Attraversa l’array NumPy e importa i dati utilizzando Aspose.Cells for Python via .NET.
import numpy as np
from aspose.cells import Workbook, Worksheet
def put_value(cells, raw_value, row , column):
cell = cells.get(row , column)
dtype = type(raw_value)
match dtype:
case np.bool_ :
value = bool(raw_value)
case np.int_ :
value = int(raw_value)
case np.intc :
value = int(raw_value)
case np.intp :
value = int(raw_value)
case np.int8 :
value = int(raw_value)
case np.int16 :
value = int(raw_value)
case np.int32 :
value = int(raw_value)
case np.int64 :
value = int(raw_value)
case np.uint8 :
value = int(raw_value)
case np.uint16 :
value = int(raw_value)
case np.uint32 :
value = int(raw_value)
case np.uint64 :
value = int(raw_value)
case np.float_:
value = int(raw_value)
case np.float16:
value = float(raw_value)
case np.float32:
value = float(raw_value)
case np.float64:
value = float(raw_value)
case np.single:
value = float(raw_value)
case np.double:
value = float(raw_value)
case np.datetime64 :
ts = pd.to_datetime(str(raw_value))
value = ts.strftime('%Y.%m.%d')
case _:
value = raw_value
cell.put_value(value)
pass
# Create a sample NumPy array
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Create a new Aspose.Cells Workbook
workbook = Workbook()
# Access the first (default) worksheet
worksheet = workbook.worksheets[0]
# Get the cells
cells = worksheet.cells
# Import data from NumPy array to the worksheet
rowindex = -1
colindex = 0
for row in data:
rowindex += 1
colindex = 0
for item in row:
put_value(cells, item, rowindex, colindex)
print(item)
colindex += 1
# Save the Excel file
workbook.save("out.xlsx")